Move and rename hlk_sw16 base entity to separate module (#126096)
This commit is contained in:
parent
c8e2408f82
commit
c20d07c14a
3 changed files with 63 additions and 57 deletions
|
@ -9,11 +9,7 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SWITCHES, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import (
|
||||
|
@ -131,53 +127,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
if not hass.data[DOMAIN]:
|
||||
hass.data.pop(DOMAIN)
|
||||
return unload_ok
|
||||
|
||||
|
||||
class SW16Device(Entity):
|
||||
"""Representation of a HLK-SW16 device.
|
||||
|
||||
Contains the common logic for HLK-SW16 entities.
|
||||
"""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(self, device_port, entry_id, client):
|
||||
"""Initialize the device."""
|
||||
# HLK-SW16 specific attributes for every component type
|
||||
self._entry_id = entry_id
|
||||
self._device_port = device_port
|
||||
self._is_on = None
|
||||
self._client = client
|
||||
self._attr_name = device_port
|
||||
self._attr_unique_id = f"{self._entry_id}_{self._device_port}"
|
||||
|
||||
@callback
|
||||
def handle_event_callback(self, event):
|
||||
"""Propagate changes through ha."""
|
||||
_LOGGER.debug("Relay %s new state callback: %r", self.unique_id, event)
|
||||
self._is_on = event
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return bool(self._client.is_connected)
|
||||
|
||||
@callback
|
||||
def _availability_callback(self, availability):
|
||||
"""Update availability state."""
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register update callback."""
|
||||
self._client.register_status_callback(
|
||||
self.handle_event_callback, self._device_port
|
||||
)
|
||||
self._is_on = await self._client.status(self._device_port)
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"hlk_sw16_device_available_{self._entry_id}",
|
||||
self._availability_callback,
|
||||
)
|
||||
)
|
||||
|
|
59
homeassistant/components/hlk_sw16/entity.py
Normal file
59
homeassistant/components/hlk_sw16/entity.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
"""Support for HLK-SW16 relay switches."""
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SW16Entity(Entity):
|
||||
"""Representation of a HLK-SW16 device.
|
||||
|
||||
Contains the common logic for HLK-SW16 entities.
|
||||
"""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(self, device_port, entry_id, client):
|
||||
"""Initialize the device."""
|
||||
# HLK-SW16 specific attributes for every component type
|
||||
self._entry_id = entry_id
|
||||
self._device_port = device_port
|
||||
self._is_on = None
|
||||
self._client = client
|
||||
self._attr_name = device_port
|
||||
self._attr_unique_id = f"{self._entry_id}_{self._device_port}"
|
||||
|
||||
@callback
|
||||
def handle_event_callback(self, event):
|
||||
"""Propagate changes through ha."""
|
||||
_LOGGER.debug("Relay %s new state callback: %r", self.unique_id, event)
|
||||
self._is_on = event
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return bool(self._client.is_connected)
|
||||
|
||||
@callback
|
||||
def _availability_callback(self, availability):
|
||||
"""Update availability state."""
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register update callback."""
|
||||
self._client.register_status_callback(
|
||||
self.handle_event_callback, self._device_port
|
||||
)
|
||||
self._is_on = await self._client.status(self._device_port)
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"hlk_sw16_device_available_{self._entry_id}",
|
||||
self._availability_callback,
|
||||
)
|
||||
)
|
|
@ -7,8 +7,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import DATA_DEVICE_REGISTER, SW16Device
|
||||
from . import DATA_DEVICE_REGISTER
|
||||
from .const import DOMAIN
|
||||
from .entity import SW16Entity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
@ -31,7 +32,7 @@ async def async_setup_entry(
|
|||
async_add_entities(devices_from_entities(hass, entry))
|
||||
|
||||
|
||||
class SW16Switch(SW16Device, SwitchEntity):
|
||||
class SW16Switch(SW16Entity, SwitchEntity):
|
||||
"""Representation of a HLK-SW16 switch."""
|
||||
|
||||
@property
|
||||
|
|
Loading…
Add table
Reference in a new issue