From 5262f44b8101bc06389a7ccb9a188af57129b8fb Mon Sep 17 00:00:00 2001 From: Jafar Atili Date: Wed, 28 Sep 2022 18:46:40 +0300 Subject: [PATCH] Refactor duplicate code in switchbee (#79209) * Refactored duplicate code * Update homeassistant/components/switchbee/entity.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/switchbee/entity.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/switchbee/entity.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * more Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- homeassistant/components/switchbee/entity.py | 25 ++++++++++++++++++++ homeassistant/components/switchbee/light.py | 23 ++---------------- homeassistant/components/switchbee/switch.py | 23 ++---------------- 3 files changed, 29 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/switchbee/entity.py b/homeassistant/components/switchbee/entity.py index f4f5163a3c9..516932d6f4e 100644 --- a/homeassistant/components/switchbee/entity.py +++ b/homeassistant/components/switchbee/entity.py @@ -1,4 +1,5 @@ """Support for SwitchBee entity.""" +import logging from typing import Generic, TypeVar from switchbee import SWITCHBEE_BRAND @@ -14,6 +15,9 @@ from .coordinator import SwitchBeeCoordinator _DeviceTypeT = TypeVar("_DeviceTypeT", bound=SwitchBeeBaseDevice) +_LOGGER = logging.getLogger(__name__) + + class SwitchBeeEntity(CoordinatorEntity[SwitchBeeCoordinator], Generic[_DeviceTypeT]): """Representation of a Switchbee entity.""" @@ -83,3 +87,24 @@ class SwitchBeeDeviceEntity(SwitchBeeEntity[_DeviceTypeT]): return except SwitchBeeError: return + + def _check_if_became_offline(self) -> None: + """Check if the device was online (now offline), log message and mark it as Unavailable.""" + # This specific call will refresh the state of the device in the CU + self.hass.async_create_task(self.async_refresh_state()) + + if self._is_online: + _LOGGER.warning( + "%s device is not responding, check the status in the SwitchBee mobile app", + self.name, + ) + self._is_online = False + + def _check_if_became_online(self) -> None: + """Check if the device was offline (now online) and bring it back.""" + if not self._is_online: + _LOGGER.info( + "%s device is now responding", + self.name, + ) + self._is_online = True diff --git a/homeassistant/components/switchbee/light.py b/homeassistant/components/switchbee/light.py index d42d5dc4c5f..4740da4cbbe 100644 --- a/homeassistant/components/switchbee/light.py +++ b/homeassistant/components/switchbee/light.py @@ -2,7 +2,6 @@ from __future__ import annotations -import logging from typing import Any, cast from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError @@ -20,8 +19,6 @@ from .entity import SwitchBeeDeviceEntity MAX_BRIGHTNESS = 255 -_LOGGER = logging.getLogger(__name__) - def _hass_brightness_to_switchbee(value: int) -> int: """Convert hass brightness to SwitchBee.""" @@ -82,26 +79,10 @@ class SwitchBeeLightEntity(SwitchBeeDeviceEntity[SwitchBeeDimmer], LightEntity): # module is offline if brightness == -1: - # This specific call will refresh the state of the device in the CU - self.hass.async_create_task(self.async_refresh_state()) - - # if the device was online (now offline), log message and mark it as Unavailable - if self._is_online: - _LOGGER.warning( - "%s light is not responding, check the status in the SwitchBee mobile app", - self.name, - ) - self._is_online = False - + self._check_if_became_offline() return - # check if the device was offline (now online) and bring it back - if not self._is_online: - _LOGGER.info( - "%s light is now responding", - self.name, - ) - self._is_online = True + self._check_if_became_online() self._attr_is_on = bool(brightness != 0) diff --git a/homeassistant/components/switchbee/switch.py b/homeassistant/components/switchbee/switch.py index 2b408513495..bb0a6123de2 100644 --- a/homeassistant/components/switchbee/switch.py +++ b/homeassistant/components/switchbee/switch.py @@ -2,7 +2,6 @@ from __future__ import annotations -import logging from typing import Any, TypeVar, Union, cast from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError @@ -24,8 +23,6 @@ from .const import DOMAIN from .coordinator import SwitchBeeCoordinator from .entity import SwitchBeeDeviceEntity -_LOGGER = logging.getLogger(__name__) - _DeviceTypeT = TypeVar( "_DeviceTypeT", bound=Union[ @@ -83,26 +80,10 @@ class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity[_DeviceTypeT], SwitchEntity): if coordinator_device.state == -1: - # This specific call will refresh the state of the device in the CU - self.hass.async_create_task(self.async_refresh_state()) - - # if the device was online (now offline), log message and mark it as Unavailable - if self._is_online: - _LOGGER.error( - "%s switch is not responding, check the status in the SwitchBee mobile app", - self.name, - ) - self._is_online = False - + self._check_if_became_offline() return - # check if the device was offline (now online) and bring it back - if not self._is_online: - _LOGGER.info( - "%s switch is now responding", - self.name, - ) - self._is_online = True + self._check_if_became_online() # timed power switch state is an integer representing the number of minutes left until it goes off # regulare switches state is ON/OFF (1/0 respectively)