1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00

Add typing to Lutron platforms (#107408)

* Add typing to Lutron platforms

* Update homeassistant/components/lutron/switch.py

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>

* Update homeassistant/components/lutron/__init__.py

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>

* Update homeassistant/components/lutron/entity.py

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>

* Update homeassistant/components/lutron/scene.py

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>

* Fix typing

* Fix typing

---------

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
Joost Lekkerkerker 2024-01-07 17:48:23 +01:00 committed by GitHub
parent 3139e92696
commit 15ce70606f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 24 deletions

View File

@ -2,7 +2,7 @@
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from pylutron import Button, Led, Lutron, OccupancyGroup, Output from pylutron import Button, Keypad, Led, Lutron, LutronEvent, OccupancyGroup, Output
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
@ -110,7 +110,9 @@ class LutronButton:
represented as an entity; it simply fires events. represented as an entity; it simply fires events.
""" """
def __init__(self, hass: HomeAssistant, area_name, keypad, button) -> None: def __init__(
self, hass: HomeAssistant, area_name: str, keypad: Keypad, button: Button
) -> None:
"""Register callback for activity on the button.""" """Register callback for activity on the button."""
name = f"{keypad.name}: {button.name}" name = f"{keypad.name}: {button.name}"
if button.name == "Unknown Button": if button.name == "Unknown Button":
@ -130,7 +132,9 @@ class LutronButton:
button.subscribe(self.button_callback, None) button.subscribe(self.button_callback, None)
def button_callback(self, button, context, event, params): def button_callback(
self, _button: Button, _context: None, event: LutronEvent, _params: dict
) -> None:
"""Fire an event about a button being pressed or released.""" """Fire an event about a button being pressed or released."""
# Events per button type: # Events per button type:
# RaiseLower -> pressed/released # RaiseLower -> pressed/released
@ -154,17 +158,17 @@ class LutronButton:
self._hass.bus.fire(self._event, data) self._hass.bus.fire(self._event, data)
@dataclass(slots=True) @dataclass(slots=True, kw_only=True)
class LutronData: class LutronData:
"""Storage class for platform global data.""" """Storage class for platform global data."""
client: Lutron client: Lutron
covers: list[tuple[str, Output]]
lights: list[tuple[str, Output]]
switches: list[tuple[str, Output]]
scenes: list[tuple[str, str, Button, Led]]
binary_sensors: list[tuple[str, OccupancyGroup]] binary_sensors: list[tuple[str, OccupancyGroup]]
buttons: list[LutronButton] buttons: list[LutronButton]
covers: list[tuple[str, Output]]
lights: list[tuple[str, Output]]
scenes: list[tuple[str, str, Button, Led]]
switches: list[tuple[str, Output]]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
@ -181,12 +185,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
entry_data = LutronData( entry_data = LutronData(
client=lutron_client, client=lutron_client,
covers=[],
lights=[],
switches=[],
scenes=[],
binary_sensors=[], binary_sensors=[],
buttons=[], buttons=[],
covers=[],
lights=[],
scenes=[],
switches=[],
) )
# Sort our devices into types # Sort our devices into types
_LOGGER.debug("Start adding devices") _LOGGER.debug("Start adding devices")

View File

@ -49,6 +49,7 @@ class LutronOccupancySensor(LutronDevice, BinarySensorEntity):
reported as a single occupancy group. reported as a single occupancy group.
""" """
_lutron_device: OccupancyGroup
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY _attr_device_class = BinarySensorDeviceClass.OCCUPANCY
@property @property

View File

@ -5,6 +5,8 @@ from collections.abc import Mapping
import logging import logging
from typing import Any from typing import Any
from pylutron import Output
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_POSITION, ATTR_POSITION,
CoverEntity, CoverEntity,
@ -33,7 +35,7 @@ async def async_setup_entry(
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id] entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities( async_add_entities(
[ [
LutronCover(area_name, device, entry_data.covers) LutronCover(area_name, device, entry_data.client)
for area_name, device in entry_data.covers for area_name, device in entry_data.covers
], ],
True, True,
@ -48,6 +50,7 @@ class LutronCover(LutronDevice, CoverEntity):
| CoverEntityFeature.CLOSE | CoverEntityFeature.CLOSE
| CoverEntityFeature.SET_POSITION | CoverEntityFeature.SET_POSITION
) )
_lutron_device: Output
@property @property
def is_closed(self) -> bool: def is_closed(self) -> bool:

View File

@ -1,4 +1,7 @@
"""Base class for Lutron devices.""" """Base class for Lutron devices."""
from pylutron import Lutron, LutronEntity, LutronEvent
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
@ -7,7 +10,9 @@ class LutronDevice(Entity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, area_name, lutron_device, controller) -> None: def __init__(
self, area_name: str, lutron_device: LutronEntity, controller: Lutron
) -> None:
"""Initialize the device.""" """Initialize the device."""
self._lutron_device = lutron_device self._lutron_device = lutron_device
self._controller = controller self._controller = controller
@ -17,7 +22,9 @@ class LutronDevice(Entity):
"""Register callbacks.""" """Register callbacks."""
self._lutron_device.subscribe(self._update_callback, None) self._lutron_device.subscribe(self._update_callback, None)
def _update_callback(self, _device, _context, _event, _params): def _update_callback(
self, _device: LutronEntity, _context: None, _event: LutronEvent, _params: dict
) -> None:
"""Run when invoked by pylutron when the device state changes.""" """Run when invoked by pylutron when the device state changes."""
self.schedule_update_ha_state() self.schedule_update_ha_state()
@ -27,7 +34,7 @@ class LutronDevice(Entity):
return f"{self._area_name} {self._lutron_device.name}" return f"{self._area_name} {self._lutron_device.name}"
@property @property
def unique_id(self): def unique_id(self) -> str | None:
"""Return a unique ID.""" """Return a unique ID."""
# Temporary fix for https://github.com/thecynic/pylutron/issues/70 # Temporary fix for https://github.com/thecynic/pylutron/issues/70
if self._lutron_device.uuid is None: if self._lutron_device.uuid is None:

View File

@ -4,6 +4,8 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
from typing import Any from typing import Any
from pylutron import Output
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -48,11 +50,8 @@ class LutronLight(LutronDevice, LightEntity):
_attr_color_mode = ColorMode.BRIGHTNESS _attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS} _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
_lutron_device: Output
def __init__(self, area_name, lutron_device, controller) -> None: _prev_brightness: int | None = None
"""Initialize the light."""
self._prev_brightness = None
super().__init__(area_name, lutron_device, controller)
@property @property
def brightness(self) -> int: def brightness(self) -> int:

View File

@ -3,6 +3,8 @@ from __future__ import annotations
from typing import Any from typing import Any
from pylutron import Button, Led, Lutron
from homeassistant.components.scene import Scene from homeassistant.components.scene import Scene
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -36,7 +38,16 @@ async def async_setup_entry(
class LutronScene(LutronDevice, Scene): class LutronScene(LutronDevice, Scene):
"""Representation of a Lutron Scene.""" """Representation of a Lutron Scene."""
def __init__(self, area_name, keypad_name, lutron_device, lutron_led, controller): _lutron_device: Button
def __init__(
self,
area_name: str,
keypad_name: str,
lutron_device: Button,
lutron_led: Led,
controller: Lutron,
) -> None:
"""Initialize the scene/button.""" """Initialize the scene/button."""
super().__init__(area_name, lutron_device, controller) super().__init__(area_name, lutron_device, controller)
self._keypad_name = keypad_name self._keypad_name = keypad_name

View File

@ -4,6 +4,8 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
from typing import Any from typing import Any
from pylutron import Button, Led, Lutron, Output
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -42,7 +44,11 @@ async def async_setup_entry(
class LutronSwitch(LutronDevice, SwitchEntity): class LutronSwitch(LutronDevice, SwitchEntity):
"""Representation of a Lutron Switch.""" """Representation of a Lutron Switch."""
def __init__(self, area_name, lutron_device, controller) -> None: _lutron_device: Output
def __init__(
self, area_name: str, lutron_device: Output, controller: Lutron
) -> None:
"""Initialize the switch.""" """Initialize the switch."""
self._prev_state = None self._prev_state = None
super().__init__(area_name, lutron_device, controller) super().__init__(area_name, lutron_device, controller)
@ -74,7 +80,16 @@ class LutronSwitch(LutronDevice, SwitchEntity):
class LutronLed(LutronDevice, SwitchEntity): class LutronLed(LutronDevice, SwitchEntity):
"""Representation of a Lutron Keypad LED.""" """Representation of a Lutron Keypad LED."""
def __init__(self, area_name, keypad_name, scene_device, led_device, controller): _lutron_device: Led
def __init__(
self,
area_name: str,
keypad_name: str,
scene_device: Button,
led_device: Led,
controller: Lutron,
) -> None:
"""Initialize the switch.""" """Initialize the switch."""
self._keypad_name = keypad_name self._keypad_name = keypad_name
self._scene_name = scene_device.name self._scene_name = scene_device.name