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>
pull/79217/head
Jafar Atili 2022-09-28 18:46:40 +03:00 committed by GitHub
parent b173ae7f44
commit 5262f44b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 42 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)