From 9672ca5719ea06d761e59022515c0f8175e11651 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Fri, 5 Jan 2024 20:38:02 +0100 Subject: [PATCH] Extract LutronDevice into separate file (#107285) --- homeassistant/components/lutron/__init__.py | 34 ------------------ .../components/lutron/binary_sensor.py | 3 +- homeassistant/components/lutron/cover.py | 3 +- homeassistant/components/lutron/entity.py | 35 +++++++++++++++++++ homeassistant/components/lutron/light.py | 3 +- homeassistant/components/lutron/scene.py | 3 +- homeassistant/components/lutron/switch.py | 3 +- 7 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 homeassistant/components/lutron/entity.py diff --git a/homeassistant/components/lutron/__init__.py b/homeassistant/components/lutron/__init__.py index 3a24c0eb0e8..27d55b8c936 100644 --- a/homeassistant/components/lutron/__init__.py +++ b/homeassistant/components/lutron/__init__.py @@ -16,7 +16,6 @@ from homeassistant.const import ( from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant from homeassistant.data_entry_flow import FlowResultType import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.entity import Entity from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.typing import ConfigType from homeassistant.util import slugify @@ -178,39 +177,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) -class LutronDevice(Entity): - """Representation of a Lutron device entity.""" - - _attr_should_poll = False - - def __init__(self, area_name, lutron_device, controller) -> None: - """Initialize the device.""" - self._lutron_device = lutron_device - self._controller = controller - self._area_name = area_name - - async def async_added_to_hass(self) -> None: - """Register callbacks.""" - self._lutron_device.subscribe(self._update_callback, None) - - def _update_callback(self, _device, _context, _event, _params): - """Run when invoked by pylutron when the device state changes.""" - self.schedule_update_ha_state() - - @property - def name(self) -> str: - """Return the name of the device.""" - return f"{self._area_name} {self._lutron_device.name}" - - @property - def unique_id(self): - """Return a unique ID.""" - # Temporary fix for https://github.com/thecynic/pylutron/issues/70 - if self._lutron_device.uuid is None: - return None - return f"{self._controller.guid}_{self._lutron_device.uuid}" - - class LutronButton: """Representation of a button on a Lutron keypad. diff --git a/homeassistant/components/lutron/binary_sensor.py b/homeassistant/components/lutron/binary_sensor.py index 1b32b009f01..1a552c539e5 100644 --- a/homeassistant/components/lutron/binary_sensor.py +++ b/homeassistant/components/lutron/binary_sensor.py @@ -16,7 +16,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import DiscoveryInfoType -from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice +from . import LUTRON_CONTROLLER, LUTRON_DEVICES +from .entity import LutronDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/lutron/cover.py b/homeassistant/components/lutron/cover.py index a3b977b9bb3..230722a7618 100644 --- a/homeassistant/components/lutron/cover.py +++ b/homeassistant/components/lutron/cover.py @@ -14,7 +14,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice +from . import LUTRON_CONTROLLER, LUTRON_DEVICES +from .entity import LutronDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/lutron/entity.py b/homeassistant/components/lutron/entity.py new file mode 100644 index 00000000000..238c2e3c552 --- /dev/null +++ b/homeassistant/components/lutron/entity.py @@ -0,0 +1,35 @@ +"""Base class for Lutron devices.""" +from homeassistant.helpers.entity import Entity + + +class LutronDevice(Entity): + """Representation of a Lutron device entity.""" + + _attr_should_poll = False + + def __init__(self, area_name, lutron_device, controller) -> None: + """Initialize the device.""" + self._lutron_device = lutron_device + self._controller = controller + self._area_name = area_name + + async def async_added_to_hass(self) -> None: + """Register callbacks.""" + self._lutron_device.subscribe(self._update_callback, None) + + def _update_callback(self, _device, _context, _event, _params): + """Run when invoked by pylutron when the device state changes.""" + self.schedule_update_ha_state() + + @property + def name(self) -> str: + """Return the name of the device.""" + return f"{self._area_name} {self._lutron_device.name}" + + @property + def unique_id(self): + """Return a unique ID.""" + # Temporary fix for https://github.com/thecynic/pylutron/issues/70 + if self._lutron_device.uuid is None: + return None + return f"{self._controller.guid}_{self._lutron_device.uuid}" diff --git a/homeassistant/components/lutron/light.py b/homeassistant/components/lutron/light.py index c6e54675ffd..2c794314fc0 100644 --- a/homeassistant/components/lutron/light.py +++ b/homeassistant/components/lutron/light.py @@ -9,7 +9,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice +from . import LUTRON_CONTROLLER, LUTRON_DEVICES +from .entity import LutronDevice async def async_setup_entry( diff --git a/homeassistant/components/lutron/scene.py b/homeassistant/components/lutron/scene.py index ed4e28d945a..5e0b7920270 100644 --- a/homeassistant/components/lutron/scene.py +++ b/homeassistant/components/lutron/scene.py @@ -8,7 +8,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice +from . import LUTRON_CONTROLLER, LUTRON_DEVICES +from .entity import LutronDevice async def async_setup_entry( diff --git a/homeassistant/components/lutron/switch.py b/homeassistant/components/lutron/switch.py index 572b599787a..45b6f1bcc25 100644 --- a/homeassistant/components/lutron/switch.py +++ b/homeassistant/components/lutron/switch.py @@ -9,7 +9,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice +from . import LUTRON_CONTROLLER, LUTRON_DEVICES +from .entity import LutronDevice async def async_setup_entry(