Extract Supla base entity into its own file (#90781)
* Extracting Supla base entity * Fix improper import * Making Black happy. * Use set for membership check * Making ruff happy.
This commit is contained in:
parent
5e3796c333
commit
e7c5325ba8
4 changed files with 74 additions and 67 deletions
|
@ -14,10 +14,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.discovery import async_load_platform
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -155,58 +152,3 @@ async def discover_devices(hass, hass_config):
|
|||
# Load discovered devices
|
||||
for component_name, config in component_configs.items():
|
||||
await async_load_platform(hass, component_name, DOMAIN, config, hass_config)
|
||||
|
||||
|
||||
class SuplaChannel(CoordinatorEntity):
|
||||
"""Base class of a Supla Channel (an equivalent of HA's Entity)."""
|
||||
|
||||
def __init__(self, config, server, coordinator):
|
||||
"""Init from config, hookup[ server and coordinator."""
|
||||
super().__init__(coordinator)
|
||||
self.server_name = config["server_name"]
|
||||
self.channel_id = config["channel_id"]
|
||||
self.server = server
|
||||
|
||||
@property
|
||||
def channel_data(self):
|
||||
"""Return channel data taken from coordinator."""
|
||||
return self.coordinator.data.get(self.channel_id)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
return "supla-{}-{}".format(
|
||||
self.channel_data["iodevice"]["gUIDString"].lower(),
|
||||
self.channel_data["channelNumber"],
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
"""Return the name of the device."""
|
||||
return self.channel_data["caption"]
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
if self.channel_data is None:
|
||||
return False
|
||||
if (state := self.channel_data.get("state")) is None:
|
||||
return False
|
||||
return state.get("connected")
|
||||
|
||||
async def async_action(self, action, **add_pars):
|
||||
"""Run server action.
|
||||
|
||||
Actions are currently hardcoded in components.
|
||||
Supla's API enables autodiscovery
|
||||
"""
|
||||
_LOGGER.debug(
|
||||
"Executing action %s on channel %d, params: %s",
|
||||
action,
|
||||
self.channel_data["id"],
|
||||
add_pars,
|
||||
)
|
||||
await self.server.execute_action(self.channel_data["id"], action, **add_pars)
|
||||
|
||||
# Update state
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
|
|
@ -10,7 +10,8 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import DOMAIN, SUPLA_COORDINATORS, SUPLA_SERVERS, SuplaChannel
|
||||
from . import DOMAIN, SUPLA_COORDINATORS, SUPLA_SERVERS
|
||||
from .entity import SuplaEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -38,7 +39,7 @@ async def async_setup_platform(
|
|||
|
||||
if device_name == SUPLA_SHUTTER:
|
||||
entities.append(
|
||||
SuplaCover(
|
||||
SuplaCoverEntity(
|
||||
device,
|
||||
hass.data[DOMAIN][SUPLA_SERVERS][server_name],
|
||||
hass.data[DOMAIN][SUPLA_COORDINATORS][server_name],
|
||||
|
@ -47,7 +48,7 @@ async def async_setup_platform(
|
|||
|
||||
elif device_name in {SUPLA_GATE, SUPLA_GARAGE_DOOR}:
|
||||
entities.append(
|
||||
SuplaDoor(
|
||||
SuplaDoorEntity(
|
||||
device,
|
||||
hass.data[DOMAIN][SUPLA_SERVERS][server_name],
|
||||
hass.data[DOMAIN][SUPLA_COORDINATORS][server_name],
|
||||
|
@ -57,7 +58,7 @@ async def async_setup_platform(
|
|||
async_add_entities(entities)
|
||||
|
||||
|
||||
class SuplaCover(SuplaChannel, CoverEntity):
|
||||
class SuplaCoverEntity(SuplaEntity, CoverEntity):
|
||||
"""Representation of a Supla Cover."""
|
||||
|
||||
@property
|
||||
|
@ -91,7 +92,7 @@ class SuplaCover(SuplaChannel, CoverEntity):
|
|||
await self.async_action("STOP")
|
||||
|
||||
|
||||
class SuplaDoor(SuplaChannel, CoverEntity):
|
||||
class SuplaDoorEntity(SuplaEntity, CoverEntity):
|
||||
"""Representation of a Supla door."""
|
||||
|
||||
@property
|
||||
|
|
63
homeassistant/components/supla/entity.py
Normal file
63
homeassistant/components/supla/entity.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
"""Base class for Supla channels."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SuplaEntity(CoordinatorEntity):
|
||||
"""Base class of a Supla Channel (an equivalent of HA's Entity)."""
|
||||
|
||||
def __init__(self, config, server, coordinator):
|
||||
"""Init from config, hookup[ server and coordinator."""
|
||||
super().__init__(coordinator)
|
||||
self.server_name = config["server_name"]
|
||||
self.channel_id = config["channel_id"]
|
||||
self.server = server
|
||||
|
||||
@property
|
||||
def channel_data(self):
|
||||
"""Return channel data taken from coordinator."""
|
||||
return self.coordinator.data.get(self.channel_id)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
return "supla-{}-{}".format(
|
||||
self.channel_data["iodevice"]["gUIDString"].lower(),
|
||||
self.channel_data["channelNumber"],
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
"""Return the name of the device."""
|
||||
return self.channel_data["caption"]
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
if self.channel_data is None:
|
||||
return False
|
||||
if (state := self.channel_data.get("state")) is None:
|
||||
return False
|
||||
return state.get("connected")
|
||||
|
||||
async def async_action(self, action, **add_pars):
|
||||
"""Run server action.
|
||||
|
||||
Actions are currently hardcoded in components.
|
||||
Supla's API enables autodiscovery
|
||||
"""
|
||||
_LOGGER.debug(
|
||||
"Executing action %s on channel %d, params: %s",
|
||||
action,
|
||||
self.channel_data["id"],
|
||||
add_pars,
|
||||
)
|
||||
await self.server.execute_action(self.channel_data["id"], action, **add_pars)
|
||||
|
||||
# Update state
|
||||
await self.coordinator.async_request_refresh()
|
|
@ -10,7 +10,8 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import DOMAIN, SUPLA_COORDINATORS, SUPLA_SERVERS, SuplaChannel
|
||||
from . import DOMAIN, SUPLA_COORDINATORS, SUPLA_SERVERS
|
||||
from .entity import SuplaEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -32,7 +33,7 @@ async def async_setup_platform(
|
|||
server_name = device["server_name"]
|
||||
|
||||
entities.append(
|
||||
SuplaSwitch(
|
||||
SuplaSwitchEntity(
|
||||
device,
|
||||
hass.data[DOMAIN][SUPLA_SERVERS][server_name],
|
||||
hass.data[DOMAIN][SUPLA_COORDINATORS][server_name],
|
||||
|
@ -42,7 +43,7 @@ async def async_setup_platform(
|
|||
async_add_entities(entities)
|
||||
|
||||
|
||||
class SuplaSwitch(SuplaChannel, SwitchEntity):
|
||||
class SuplaSwitchEntity(SuplaEntity, SwitchEntity):
|
||||
"""Representation of a Supla Switch."""
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
|
|
Loading…
Add table
Reference in a new issue