From 219417cfb549dc748c95454f6d8b3e24528d7b27 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:13:40 +0200 Subject: [PATCH] Move homeworks base entity to separate module (#126097) * Move homeworks base entity to separate module * Move calculate_unique_id to util.py --- .../components/homeworks/__init__.py | 34 ------------------ .../components/homeworks/binary_sensor.py | 3 +- homeassistant/components/homeworks/button.py | 3 +- .../components/homeworks/config_flow.py | 4 ++- homeassistant/components/homeworks/entity.py | 35 +++++++++++++++++++ homeassistant/components/homeworks/light.py | 3 +- homeassistant/components/homeworks/util.py | 6 ++++ 7 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 homeassistant/components/homeworks/entity.py create mode 100644 homeassistant/components/homeworks/util.py diff --git a/homeassistant/components/homeworks/__init__.py b/homeassistant/components/homeworks/__init__.py index 448487cb8b0051..e9e8c969b61f25 100644 --- a/homeassistant/components/homeworks/__init__.py +++ b/homeassistant/components/homeworks/__init__.py @@ -33,7 +33,6 @@ 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 @@ 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. diff --git a/homeassistant/components/homeworks/binary_sensor.py b/homeassistant/components/homeworks/binary_sensor.py index 9a9f7086ba5b25..f1ba3c02835f77 100644 --- a/homeassistant/components/homeworks/binary_sensor.py +++ b/homeassistant/components/homeworks/binary_sensor.py @@ -15,7 +15,7 @@ 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 @@ CONF_NUMBER, DOMAIN, ) +from .entity import HomeworksEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/homeworks/button.py b/homeassistant/components/homeworks/button.py index f071b05b4927f5..6a13573ac88dbc 100644 --- a/homeassistant/components/homeworks/button.py +++ b/homeassistant/components/homeworks/button.py @@ -13,7 +13,7 @@ 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 @@ CONF_RELEASE_DELAY, DOMAIN, ) +from .entity import HomeworksEntity async def async_setup_entry( diff --git a/homeassistant/components/homeworks/config_flow.py b/homeassistant/components/homeworks/config_flow.py index 8e9c8e3b29a77c..3d947e3d599f4d 100644 --- a/homeassistant/components/homeworks/config_flow.py +++ b/homeassistant/components/homeworks/config_flow.py @@ -39,7 +39,6 @@ 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 @@ 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( diff --git a/homeassistant/components/homeworks/entity.py b/homeassistant/components/homeworks/entity.py new file mode 100644 index 00000000000000..49abfb9241ea72 --- /dev/null +++ b/homeassistant/components/homeworks/entity.py @@ -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} diff --git a/homeassistant/components/homeworks/light.py b/homeassistant/components/homeworks/light.py index 20ae08017d3f90..ac52c1f4974bb8 100644 --- a/homeassistant/components/homeworks/light.py +++ b/homeassistant/components/homeworks/light.py @@ -15,8 +15,9 @@ 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__) diff --git a/homeassistant/components/homeworks/util.py b/homeassistant/components/homeworks/util.py new file mode 100644 index 00000000000000..0ed295f7baeb1e --- /dev/null +++ b/homeassistant/components/homeworks/util.py @@ -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}"