Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
hass object is required for schedule_update_ha_state
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus committed Mar 10, 2021
1 parent ccc35e8 commit cb5ccd8
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions custom_components/deebot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
COMPONENT_MAIN_BRUSH,
)
from homeassistant.const import STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from .const import DOMAIN

Expand All @@ -21,18 +22,18 @@ async def async_setup_entry(hass, config_entry, async_add_devices):
new_devices = []
for vacbot in hub.vacbots:
# General
new_devices.append(DeebotLastCleanImageSensor(vacbot, "last_clean_image"))
new_devices.append(DeebotWaterLevelSensor(vacbot, "water_level"))
new_devices.append(DeebotLastCleanImageSensor(vacbot, "last_clean_image", hass))
new_devices.append(DeebotWaterLevelSensor(vacbot, "water_level", hass))

# Components
new_devices.append(DeebotComponentSensor(vacbot, COMPONENT_MAIN_BRUSH))
new_devices.append(DeebotComponentSensor(vacbot, COMPONENT_SIDE_BRUSH))
new_devices.append(DeebotComponentSensor(vacbot, COMPONENT_FILTER))
new_devices.append(DeebotComponentSensor(vacbot, COMPONENT_MAIN_BRUSH, hass))
new_devices.append(DeebotComponentSensor(vacbot, COMPONENT_SIDE_BRUSH, hass))
new_devices.append(DeebotComponentSensor(vacbot, COMPONENT_FILTER, hass))

# Stats
new_devices.append(DeebotStatsSensor(vacbot, "stats_area"))
new_devices.append(DeebotStatsSensor(vacbot, "stats_time"))
new_devices.append(DeebotStatsSensor(vacbot, "stats_type"))
new_devices.append(DeebotStatsSensor(vacbot, "stats_area", hass))
new_devices.append(DeebotStatsSensor(vacbot, "stats_time", hass))
new_devices.append(DeebotStatsSensor(vacbot, "stats_type", hass))

if new_devices:
async_add_devices(new_devices)
Expand All @@ -41,12 +42,13 @@ async def async_setup_entry(hass, config_entry, async_add_devices):
class DeebotBaseSensor(Entity):
"""Deebot base sensor"""

def __init__(self, vacbot, device_id):
def __init__(self, vacbot, device_id, hass: HomeAssistant):
"""Initialize the Sensor."""

self._state = STATE_UNKNOWN
self._vacbot = vacbot
self._id = device_id
self.hass = hass

if self._vacbot.vacuum.get("nick", None) is not None:
self._vacbot_name = "{}".format(self._vacbot.vacuum["nick"])
Expand Down Expand Up @@ -75,9 +77,9 @@ def entity_registry_enabled_default(self) -> bool:
class DeebotLastCleanImageSensor(DeebotBaseSensor):
"""Deebot Sensor"""

def __init__(self, vacbot, device_id):
def __init__(self, vacbot, device_id, hass: HomeAssistant):
"""Initialize the Sensor."""
super(DeebotLastCleanImageSensor, self).__init__(vacbot, device_id)
super(DeebotLastCleanImageSensor, self).__init__(vacbot, device_id, hass)
self._vacbot.cleanLogsEvents.subscribe(lambda _: self.schedule_update_ha_state())

@property
Expand All @@ -95,9 +97,9 @@ def icon(self) -> Optional[str]:
class DeebotWaterLevelSensor(DeebotBaseSensor):
"""Deebot Sensor"""

def __init__(self, vacbot, device_id):
def __init__(self, vacbot, device_id, hass: HomeAssistant):
"""Initialize the Sensor."""
super(DeebotWaterLevelSensor, self).__init__(vacbot, device_id)
super(DeebotWaterLevelSensor, self).__init__(vacbot, device_id, hass)
self._vacbot.waterEvents.subscribe(lambda _: self.schedule_update_ha_state())

@property
Expand All @@ -116,9 +118,9 @@ def icon(self) -> Optional[str]:
class DeebotComponentSensor(DeebotBaseSensor):
"""Deebot Sensor"""

def __init__(self, vacbot, device_id):
def __init__(self, vacbot, device_id, hass: HomeAssistant):
"""Initialize the Sensor."""
super(DeebotComponentSensor, self).__init__(vacbot, device_id)
super(DeebotComponentSensor, self).__init__(vacbot, device_id, hass)
self._vacbot.lifespanEvents.subscribe(lambda _: self.schedule_update_ha_state())

@property
Expand Down Expand Up @@ -146,9 +148,9 @@ def icon(self) -> Optional[str]:
class DeebotStatsSensor(DeebotBaseSensor):
"""Deebot Sensor"""

def __init__(self, vacbot, device_id):
def __init__(self, vacbot, device_id, hass: HomeAssistant):
"""Initialize the Sensor."""
super(DeebotStatsSensor, self).__init__(vacbot, device_id)
super(DeebotStatsSensor, self).__init__(vacbot, device_id, hass)
self._vacbot.statsEvents.subscribe(lambda _: self.schedule_update_ha_state())

@property
Expand Down

0 comments on commit cb5ccd8

Please sign in to comment.