Move homeworks base entity to separate module (#126097)

* Move homeworks base entity to separate module

* Move calculate_unique_id to util.py
This commit is contained in:
epenet 2024-09-17 16:13:40 +02:00 committed by GitHub
parent ca59805907
commit 219417cfb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 50 additions and 38 deletions

View File

@ -33,7 +33,6 @@ from homeassistant.exceptions import ConfigEntryNotReady, ServiceValidationError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import slugify
@ -48,8 +47,6 @@ CONF_COMMAND = "command"
EVENT_BUTTON_PRESS = "homeworks_button_press"
EVENT_BUTTON_RELEASE = "homeworks_button_release"
DEFAULT_FADE_RATE = 1.0
KEYPAD_LEDSTATE_POLL_COOLDOWN = 1.0
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
@ -204,37 +201,6 @@ async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
await hass.config_entries.async_reload(entry.entry_id)
def calculate_unique_id(controller_id: str, addr: str, idx: int) -> str:
"""Calculate entity unique id."""
return f"homeworks.{controller_id}.{addr}.{idx}"
class HomeworksEntity(Entity):
"""Base class of a Homeworks device."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
controller: Homeworks,
controller_id: str,
addr: str,
idx: int,
name: str | None,
) -> None:
"""Initialize Homeworks device."""
self._addr = addr
self._idx = idx
self._controller_id = controller_id
self._attr_name = name
self._attr_unique_id = calculate_unique_id(
self._controller_id, self._addr, self._idx
)
self._controller = controller
self._attr_extra_state_attributes = {"homeworks_address": self._addr}
class HomeworksKeypad:
"""When you want signals instead of entities.

View File

@ -15,7 +15,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeworksData, HomeworksEntity, HomeworksKeypad
from . import HomeworksData, HomeworksKeypad
from .const import (
CONF_ADDR,
CONF_BUTTONS,
@ -25,6 +25,7 @@ from .const import (
CONF_NUMBER,
DOMAIN,
)
from .entity import HomeworksEntity
_LOGGER = logging.getLogger(__name__)

View File

@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeworksData, HomeworksEntity
from . import HomeworksData
from .const import (
CONF_ADDR,
CONF_BUTTONS,
@ -23,6 +23,7 @@ from .const import (
CONF_RELEASE_DELAY,
DOMAIN,
)
from .entity import HomeworksEntity
async def async_setup_entry(

View File

@ -39,7 +39,6 @@ from homeassistant.helpers.selector import TextSelector
from homeassistant.helpers.typing import VolDictType
from homeassistant.util import slugify
from . import DEFAULT_FADE_RATE, calculate_unique_id
from .const import (
CONF_ADDR,
CONF_BUTTONS,
@ -56,9 +55,12 @@ from .const import (
DEFAULT_LIGHT_NAME,
DOMAIN,
)
from .util import calculate_unique_id
_LOGGER = logging.getLogger(__name__)
DEFAULT_FADE_RATE = 1.0
CONTROLLER_EDIT = {
vol.Required(CONF_HOST): selector.TextSelector(),
vol.Required(CONF_PORT): selector.NumberSelector(

View File

@ -0,0 +1,35 @@
"""Support for Lutron Homeworks Series 4 and 8 systems."""
from __future__ import annotations
from pyhomeworks.pyhomeworks import Homeworks
from homeassistant.helpers.entity import Entity
from .util import calculate_unique_id
class HomeworksEntity(Entity):
"""Base class of a Homeworks device."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
controller: Homeworks,
controller_id: str,
addr: str,
idx: int,
name: str | None,
) -> None:
"""Initialize Homeworks device."""
self._addr = addr
self._idx = idx
self._controller_id = controller_id
self._attr_name = name
self._attr_unique_id = calculate_unique_id(
self._controller_id, self._addr, self._idx
)
self._controller = controller
self._attr_extra_state_attributes = {"homeworks_address": self._addr}

View File

@ -15,8 +15,9 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeworksData, HomeworksEntity
from . import HomeworksData
from .const import CONF_ADDR, CONF_CONTROLLER_ID, CONF_DIMMERS, CONF_RATE, DOMAIN
from .entity import HomeworksEntity
_LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,6 @@
"""Support for Lutron Homeworks Series 4 and 8 systems."""
def calculate_unique_id(controller_id: str, addr: str, idx: int) -> str:
"""Calculate entity unique id."""
return f"homeworks.{controller_id}.{addr}.{idx}"