Skip to content

Commit

Permalink
Move homeworks base entity to separate module (#126097)
Browse files Browse the repository at this point in the history
* Move homeworks base entity to separate module

* Move calculate_unique_id to util.py
  • Loading branch information
epenet authored Sep 17, 2024
1 parent ca59805 commit 219417c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 38 deletions.
34 changes: 0 additions & 34 deletions homeassistant/components/homeworks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/homeworks/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,6 +25,7 @@
CONF_NUMBER,
DOMAIN,
)
from .entity import HomeworksEntity

_LOGGER = logging.getLogger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/homeworks/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,6 +23,7 @@
CONF_RELEASE_DELAY,
DOMAIN,
)
from .entity import HomeworksEntity


async def async_setup_entry(
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/homeworks/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
35 changes: 35 additions & 0 deletions homeassistant/components/homeworks/entity.py
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 2 additions & 1 deletion homeassistant/components/homeworks/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/homeworks/util.py
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit 219417c

Please sign in to comment.