Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 1 addition & 59 deletions homeassistant/components/supla/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
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__)

Expand Down Expand Up @@ -155,58 +152,3 @@ async def _fetch_channels():
# 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()
11 changes: 6 additions & 5 deletions homeassistant/components/supla/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
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__)

Expand Down Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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
Expand Down Expand Up @@ -91,7 +92,7 @@ async def async_stop_cover(self, **kwargs: Any) -> None:
await self.async_action("STOP")


class SuplaDoor(SuplaChannel, CoverEntity):
class SuplaDoorEntity(SuplaEntity, CoverEntity):
"""Representation of a Supla door."""

@property
Expand Down
63 changes: 63 additions & 0 deletions homeassistant/components/supla/entity.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 4 additions & 3 deletions homeassistant/components/supla/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
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__)

Expand All @@ -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],
Expand All @@ -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:
Expand Down