Move qwikswitch base entity to separate module (#126130)
This commit is contained in:
parent
01688946b3
commit
c5839604d5
6 changed files with 83 additions and 74 deletions
|
@ -9,7 +9,6 @@ from pyqwikswitch.qwikswitch import CMD_BUTTONS, QS_CMD, QS_ID, SENSORS, QSType
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA
|
from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA
|
||||||
from homeassistant.components.light import ATTR_BRIGHTNESS
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_SENSORS,
|
CONF_SENSORS,
|
||||||
CONF_SWITCHES,
|
CONF_SWITCHES,
|
||||||
|
@ -22,11 +21,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.discovery import load_platform
|
from homeassistant.helpers.discovery import load_platform
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
async_dispatcher_connect,
|
|
||||||
async_dispatcher_send,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -70,70 +65,6 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class QSEntity(Entity):
|
|
||||||
"""Qwikswitch Entity base."""
|
|
||||||
|
|
||||||
_attr_should_poll = False
|
|
||||||
|
|
||||||
def __init__(self, qsid, name):
|
|
||||||
"""Initialize the QSEntity."""
|
|
||||||
self._name = name
|
|
||||||
self.qsid = qsid
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique identifier for this sensor."""
|
|
||||||
return f"qs{self.qsid}"
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def update_packet(self, packet):
|
|
||||||
"""Receive update packet from QSUSB. Match dispather_send signature."""
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""Listen for updates from QSUSb via dispatcher."""
|
|
||||||
self.async_on_remove(
|
|
||||||
async_dispatcher_connect(self.hass, self.qsid, self.update_packet)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class QSToggleEntity(QSEntity):
|
|
||||||
"""Representation of a Qwikswitch Toggle Entity.
|
|
||||||
|
|
||||||
Implemented:
|
|
||||||
- QSLight extends QSToggleEntity and Light[2] (ToggleEntity[1])
|
|
||||||
- QSSwitch extends QSToggleEntity and SwitchEntity[3] (ToggleEntity[1])
|
|
||||||
|
|
||||||
[1] /helpers/entity.py
|
|
||||||
[2] /components/light/__init__.py
|
|
||||||
[3] /components/switch/__init__.py
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, qsid, qsusb):
|
|
||||||
"""Initialize the ToggleEntity."""
|
|
||||||
self.device = qsusb.devices[qsid]
|
|
||||||
super().__init__(qsid, self.device.name)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Check if device is on (non-zero)."""
|
|
||||||
return self.device.value > 0
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
|
||||||
"""Turn the device on."""
|
|
||||||
new = kwargs.get(ATTR_BRIGHTNESS, 255)
|
|
||||||
self.hass.data[DOMAIN].devices.set_value(self.qsid, new)
|
|
||||||
|
|
||||||
async def async_turn_off(self, **_):
|
|
||||||
"""Turn the device off."""
|
|
||||||
self.hass.data[DOMAIN].devices.set_value(self.qsid, 0)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Qwiskswitch component setup."""
|
"""Qwiskswitch component setup."""
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,8 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import DOMAIN as QWIKSWITCH, QSEntity
|
from . import DOMAIN as QWIKSWITCH
|
||||||
|
from .entity import QSEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
74
homeassistant/components/qwikswitch/entity.py
Normal file
74
homeassistant/components/qwikswitch/entity.py
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
"""Support for Qwikswitch devices."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.components.light import ATTR_BRIGHTNESS
|
||||||
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
from . import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
class QSEntity(Entity):
|
||||||
|
"""Qwikswitch Entity base."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(self, qsid, name):
|
||||||
|
"""Initialize the QSEntity."""
|
||||||
|
self._name = name
|
||||||
|
self.qsid = qsid
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return the name of the sensor."""
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return a unique identifier for this sensor."""
|
||||||
|
return f"qs{self.qsid}"
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def update_packet(self, packet):
|
||||||
|
"""Receive update packet from QSUSB. Match dispather_send signature."""
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_added_to_hass(self):
|
||||||
|
"""Listen for updates from QSUSb via dispatcher."""
|
||||||
|
self.async_on_remove(
|
||||||
|
async_dispatcher_connect(self.hass, self.qsid, self.update_packet)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class QSToggleEntity(QSEntity):
|
||||||
|
"""Representation of a Qwikswitch Toggle Entity.
|
||||||
|
|
||||||
|
Implemented:
|
||||||
|
- QSLight extends QSToggleEntity and Light[2] (ToggleEntity[1])
|
||||||
|
- QSSwitch extends QSToggleEntity and SwitchEntity[3] (ToggleEntity[1])
|
||||||
|
|
||||||
|
[1] /helpers/entity.py
|
||||||
|
[2] /components/light/__init__.py
|
||||||
|
[3] /components/switch/__init__.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, qsid, qsusb):
|
||||||
|
"""Initialize the ToggleEntity."""
|
||||||
|
self.device = qsusb.devices[qsid]
|
||||||
|
super().__init__(qsid, self.device.name)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
"""Check if device is on (non-zero)."""
|
||||||
|
return self.device.value > 0
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs):
|
||||||
|
"""Turn the device on."""
|
||||||
|
new = kwargs.get(ATTR_BRIGHTNESS, 255)
|
||||||
|
self.hass.data[DOMAIN].devices.set_value(self.qsid, new)
|
||||||
|
|
||||||
|
async def async_turn_off(self, **_):
|
||||||
|
"""Turn the device off."""
|
||||||
|
self.hass.data[DOMAIN].devices.set_value(self.qsid, 0)
|
|
@ -7,7 +7,8 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import DOMAIN as QWIKSWITCH, QSToggleEntity
|
from . import DOMAIN as QWIKSWITCH
|
||||||
|
from .entity import QSToggleEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
|
|
|
@ -12,7 +12,8 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import DOMAIN as QWIKSWITCH, QSEntity
|
from . import DOMAIN as QWIKSWITCH
|
||||||
|
from .entity import QSEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,8 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import DOMAIN as QWIKSWITCH, QSToggleEntity
|
from . import DOMAIN as QWIKSWITCH
|
||||||
|
from .entity import QSToggleEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
|
|
Loading…
Add table
Reference in a new issue