Improve devolo Home Control code quality (#40480)
Co-authored-by: Markus Bong <2Fake1987@gmail.com>
This commit is contained in:
1 parent
dc30f0e00c
commit
0b11559031
7 files changed
+100
-257
No files matched your search
@@ -6,6 +6,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .devolo_device import DevoloDeviceEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,26 +33,16 @@ async def async_setup_entry(
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class DevoloSwitch(SwitchEntity):
|
||||
class DevoloSwitch(DevoloDeviceEntity, SwitchEntity):
|
||||
"""Representation of a switch."""
|
||||
|
||||
def __init__(self, homecontrol, device_instance, element_uid):
|
||||
"""Initialize an devolo Switch."""
|
||||
self._device_instance = device_instance
|
||||
|
||||
# Create the unique ID
|
||||
self._unique_id = element_uid
|
||||
|
||||
self._homecontrol = homecontrol
|
||||
self._name = self._device_instance.item_name
|
||||
|
||||
# This is not doing I/O. It fetches an internal state of the API
|
||||
self._available = self._device_instance.is_online()
|
||||
|
||||
# Get the brand and model information
|
||||
self._brand = self._device_instance.brand
|
||||
self._model = self._device_instance.name
|
||||
|
||||
super().__init__(
|
||||
homecontrol=homecontrol,
|
||||
device_instance=device_instance,
|
||||
element_uid=element_uid,
|
||||
)
|
||||
self._binary_switch_property = self._device_instance.binary_switch_property.get(
|
||||
self._unique_id
|
||||
)
|
||||
@@ -64,47 +55,6 @@ class DevoloSwitch(SwitchEntity):
|
||||
else:
|
||||
self._consumption = None
|
||||
|
||||
self.subscriber = None
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity is added to hass."""
|
||||
self.subscriber = Subscriber(
|
||||
self._device_instance.item_name, callback=self.sync
|
||||
)
|
||||
self._homecontrol.publisher.register(
|
||||
self._device_instance.uid, self.subscriber, self.sync
|
||||
)
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID of the switch."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return the device info."""
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self._device_instance.uid)},
|
||||
"name": self.name,
|
||||
"manufacturer": self._brand,
|
||||
"model": self._model,
|
||||
}
|
||||
|
||||
@property
|
||||
def device_id(self):
|
||||
"""Return the ID of this switch."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the display name of this switch."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return the polling state."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return the state."""
|
||||
@@ -115,11 +65,6 @@ class DevoloSwitch(SwitchEntity):
|
||||
"""Return the current consumption."""
|
||||
return self._consumption
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return the online state."""
|
||||
return self._available
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Switch on the device."""
|
||||
self._is_on = True
|
||||
@@ -130,7 +75,7 @@ class DevoloSwitch(SwitchEntity):
|
||||
self._is_on = False
|
||||
self._binary_switch_property.set(state=False)
|
||||
|
||||
def sync(self, message=None):
|
||||
def _sync(self, message):
|
||||
"""Update the binary switch state and consumption."""
|
||||
if message[0].startswith("devolo.BinarySwitch"):
|
||||
self._is_on = self._device_instance.binary_switch_property[message[0]].state
|
||||
@@ -143,17 +88,3 @@ class DevoloSwitch(SwitchEntity):
|
||||
else:
|
||||
_LOGGER.debug("No valid message received: %s", message)
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
|
||||
class Subscriber:
|
||||
"""Subscriber class for the publisher in mprm websocket class."""
|
||||
|
||||
def __init__(self, name, callback):
|
||||
"""Initiate the device."""
|
||||
self.name = name
|
||||
self.callback = callback
|
||||
|
||||
def update(self, message):
|
||||
"""Trigger hass to update the device."""
|
||||
_LOGGER.debug('%s got message "%s"', self.name, message)
|
||||
self.callback(message)
|
||||
Reference in new issue
Block a user