mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
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>
This commit is contained in:
parent
b173ae7f44
commit
5262f44b81
@ -1,4 +1,5 @@
|
|||||||
"""Support for SwitchBee entity."""
|
"""Support for SwitchBee entity."""
|
||||||
|
import logging
|
||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
from switchbee import SWITCHBEE_BRAND
|
from switchbee import SWITCHBEE_BRAND
|
||||||
@ -14,6 +15,9 @@ from .coordinator import SwitchBeeCoordinator
|
|||||||
_DeviceTypeT = TypeVar("_DeviceTypeT", bound=SwitchBeeBaseDevice)
|
_DeviceTypeT = TypeVar("_DeviceTypeT", bound=SwitchBeeBaseDevice)
|
||||||
|
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class SwitchBeeEntity(CoordinatorEntity[SwitchBeeCoordinator], Generic[_DeviceTypeT]):
|
class SwitchBeeEntity(CoordinatorEntity[SwitchBeeCoordinator], Generic[_DeviceTypeT]):
|
||||||
"""Representation of a Switchbee entity."""
|
"""Representation of a Switchbee entity."""
|
||||||
|
|
||||||
@ -83,3 +87,24 @@ class SwitchBeeDeviceEntity(SwitchBeeEntity[_DeviceTypeT]):
|
|||||||
return
|
return
|
||||||
except SwitchBeeError:
|
except SwitchBeeError:
|
||||||
return
|
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
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError
|
from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError
|
||||||
@ -20,8 +19,6 @@ from .entity import SwitchBeeDeviceEntity
|
|||||||
|
|
||||||
MAX_BRIGHTNESS = 255
|
MAX_BRIGHTNESS = 255
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def _hass_brightness_to_switchbee(value: int) -> int:
|
def _hass_brightness_to_switchbee(value: int) -> int:
|
||||||
"""Convert hass brightness to SwitchBee."""
|
"""Convert hass brightness to SwitchBee."""
|
||||||
@ -82,26 +79,10 @@ class SwitchBeeLightEntity(SwitchBeeDeviceEntity[SwitchBeeDimmer], LightEntity):
|
|||||||
|
|
||||||
# module is offline
|
# module is offline
|
||||||
if brightness == -1:
|
if brightness == -1:
|
||||||
# This specific call will refresh the state of the device in the CU
|
self._check_if_became_offline()
|
||||||
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
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# check if the device was offline (now online) and bring it back
|
self._check_if_became_online()
|
||||||
if not self._is_online:
|
|
||||||
_LOGGER.info(
|
|
||||||
"%s light is now responding",
|
|
||||||
self.name,
|
|
||||||
)
|
|
||||||
self._is_online = True
|
|
||||||
|
|
||||||
self._attr_is_on = bool(brightness != 0)
|
self._attr_is_on = bool(brightness != 0)
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Any, TypeVar, Union, cast
|
from typing import Any, TypeVar, Union, cast
|
||||||
|
|
||||||
from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError
|
from switchbee.api import SwitchBeeDeviceOfflineError, SwitchBeeError
|
||||||
@ -24,8 +23,6 @@ from .const import DOMAIN
|
|||||||
from .coordinator import SwitchBeeCoordinator
|
from .coordinator import SwitchBeeCoordinator
|
||||||
from .entity import SwitchBeeDeviceEntity
|
from .entity import SwitchBeeDeviceEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
_DeviceTypeT = TypeVar(
|
_DeviceTypeT = TypeVar(
|
||||||
"_DeviceTypeT",
|
"_DeviceTypeT",
|
||||||
bound=Union[
|
bound=Union[
|
||||||
@ -83,26 +80,10 @@ class SwitchBeeSwitchEntity(SwitchBeeDeviceEntity[_DeviceTypeT], SwitchEntity):
|
|||||||
|
|
||||||
if coordinator_device.state == -1:
|
if coordinator_device.state == -1:
|
||||||
|
|
||||||
# This specific call will refresh the state of the device in the CU
|
self._check_if_became_offline()
|
||||||
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
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# check if the device was offline (now online) and bring it back
|
self._check_if_became_online()
|
||||||
if not self._is_online:
|
|
||||||
_LOGGER.info(
|
|
||||||
"%s switch is now responding",
|
|
||||||
self.name,
|
|
||||||
)
|
|
||||||
self._is_online = True
|
|
||||||
|
|
||||||
# timed power switch state is an integer representing the number of minutes left until it goes off
|
# 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)
|
# regulare switches state is ON/OFF (1/0 respectively)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user