From 267896cfc0cc0c78762ca7b9b58132f6e6ba10a0 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Sat, 4 Dec 2021 04:17:17 -0500 Subject: [PATCH] Address late review of eight_sleep (#60951) --- homeassistant/components/eight_sleep/__init__.py | 6 ++---- homeassistant/components/eight_sleep/binary_sensor.py | 3 ++- homeassistant/components/eight_sleep/sensor.py | 10 ++++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/eight_sleep/__init__.py b/homeassistant/components/eight_sleep/__init__.py index 09229ce767e..28de45392eb 100644 --- a/homeassistant/components/eight_sleep/__init__.py +++ b/homeassistant/components/eight_sleep/__init__.py @@ -96,7 +96,7 @@ CONFIG_SCHEMA = vol.Schema( ) -def _get_device_unique_id(eight: EightSleep, user_obj: EightUser = None) -> str: +def _get_device_unique_id(eight: EightSleep, user_obj: EightUser | None = None) -> str: """Get the device's unique ID.""" unique_id = eight.deviceid if user_obj: @@ -199,7 +199,6 @@ class EightSleepHeatDataCoordinator(DataUpdateCoordinator): _LOGGER, name=f"{DOMAIN}_heat", update_interval=HEAT_SCAN_INTERVAL, - update_method=self._async_update_data, ) async def _async_update_data(self) -> None: @@ -217,7 +216,6 @@ class EightSleepUserDataCoordinator(DataUpdateCoordinator): _LOGGER, name=f"{DOMAIN}_user", update_interval=USER_SCAN_INTERVAL, - update_method=self._async_update_data, ) async def _async_update_data(self) -> None: @@ -240,7 +238,7 @@ class EightSleepBaseEntity(CoordinatorEntity): self._eight = eight self._side = side self._sensor = sensor - self._usrobj: EightUser = None + self._usrobj: EightUser | None = None if self._side: self._usrobj = self._eight.users[self._eight.fetch_userid(self._side)] full_sensor_name = self._sensor diff --git a/homeassistant/components/eight_sleep/binary_sensor.py b/homeassistant/components/eight_sleep/binary_sensor.py index 7240d65d262..cb1b2e36f79 100644 --- a/homeassistant/components/eight_sleep/binary_sensor.py +++ b/homeassistant/components/eight_sleep/binary_sensor.py @@ -62,7 +62,7 @@ class EightHeatSensor(EightSleepBaseEntity, BinarySensorEntity): """Initialize the sensor.""" super().__init__(name, coordinator, eight, side, sensor) self._attr_device_class = DEVICE_CLASS_OCCUPANCY - + assert self._usrobj _LOGGER.debug( "Presence Sensor: %s, Side: %s, User: %s", self._sensor, @@ -73,4 +73,5 @@ class EightHeatSensor(EightSleepBaseEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return true if the binary sensor is on.""" + assert self._usrobj return bool(self._usrobj.bed_presence) diff --git a/homeassistant/components/eight_sleep/sensor.py b/homeassistant/components/eight_sleep/sensor.py index 42270ad4fc4..6cb2d5bf13c 100644 --- a/homeassistant/components/eight_sleep/sensor.py +++ b/homeassistant/components/eight_sleep/sensor.py @@ -106,6 +106,7 @@ class EightHeatSensor(EightSleepBaseEntity, SensorEntity): """Initialize the sensor.""" super().__init__(name, coordinator, eight, side, sensor) self._attr_native_unit_of_measurement = PERCENTAGE + assert self._usrobj _LOGGER.debug( "Heat Sensor: %s, Side: %s, User: %s", @@ -117,11 +118,13 @@ class EightHeatSensor(EightSleepBaseEntity, SensorEntity): @property def native_value(self) -> int: """Return the state of the sensor.""" + assert self._usrobj return self._usrobj.heating_level @property def extra_state_attributes(self) -> dict[str, Any]: """Return device state attributes.""" + assert self._usrobj return { ATTR_TARGET_HEAT: self._usrobj.target_heating_level, ATTR_ACTIVE_HEAT: self._usrobj.now_heating, @@ -167,6 +170,9 @@ class EightUserSensor(EightSleepUserEntity, SensorEntity): @property def native_value(self) -> str | int | float | None: """Return the state of the sensor.""" + if not self._usrobj: + return None + if "current" in self._sensor: if "fitness" in self._sensor: return self._usrobj.current_sleep_fitness_score @@ -215,12 +221,12 @@ class EightUserSensor(EightSleepUserEntity, SensorEntity): def extra_state_attributes(self) -> dict[str, Any] | None: """Return device state attributes.""" attr = None - if "current" in self._sensor: + if "current" in self._sensor and self._usrobj: if "fitness" in self._sensor: attr = self._usrobj.current_fitness_values else: attr = self._usrobj.current_values - elif "last" in self._sensor: + elif "last" in self._sensor and self._usrobj: attr = self._usrobj.last_values if attr is None: