Skip to content

Commit

Permalink
Add typing to Lutron platforms (home-assistant#107408)
Browse files Browse the repository at this point in the history
* Add typing to Lutron platforms

* Update homeassistant/components/lutron/switch.py

Co-authored-by: Marc Mueller <[email protected]>

* Update homeassistant/components/lutron/__init__.py

Co-authored-by: Marc Mueller <[email protected]>

* Update homeassistant/components/lutron/entity.py

Co-authored-by: Marc Mueller <[email protected]>

* Update homeassistant/components/lutron/scene.py

Co-authored-by: Marc Mueller <[email protected]>

* Fix typing

* Fix typing

---------

Co-authored-by: Marc Mueller <[email protected]>
  • Loading branch information
joostlek and cdce8p authored Jan 7, 2024
1 parent 3139e92 commit 15ce706
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
24 changes: 14 additions & 10 deletions homeassistant/components/lutron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass
import logging

from pylutron import Button, Led, Lutron, OccupancyGroup, Output
from pylutron import Button, Keypad, Led, Lutron, LutronEvent, OccupancyGroup, Output
import voluptuous as vol

from homeassistant import config_entries
Expand Down Expand Up @@ -110,7 +110,9 @@ class LutronButton:
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."""
name = f"{keypad.name}: {button.name}"
if button.name == "Unknown Button":
Expand All @@ -130,7 +132,9 @@ def __init__(self, hass: HomeAssistant, area_name, keypad, button) -> 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."""
# Events per button type:
# RaiseLower -> pressed/released
Expand All @@ -154,17 +158,17 @@ def button_callback(self, button, context, event, params):
self._hass.bus.fire(self._event, data)


@dataclass(slots=True)
@dataclass(slots=True, kw_only=True)
class LutronData:
"""Storage class for platform global data."""

client: Lutron
binary_sensors: list[tuple[str, OccupancyGroup]]
buttons: list[LutronButton]
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]]
buttons: list[LutronButton]
switches: list[tuple[str, Output]]


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
Expand All @@ -181,12 +185,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b

entry_data = LutronData(
client=lutron_client,
binary_sensors=[],
buttons=[],
covers=[],
lights=[],
switches=[],
scenes=[],
binary_sensors=[],
buttons=[],
switches=[],
)
# Sort our devices into types
_LOGGER.debug("Start adding devices")
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/lutron/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class LutronOccupancySensor(LutronDevice, BinarySensorEntity):
reported as a single occupancy group.
"""

_lutron_device: OccupancyGroup
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY

@property
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/lutron/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
from typing import Any

from pylutron import Output

from homeassistant.components.cover import (
ATTR_POSITION,
CoverEntity,
Expand Down Expand Up @@ -33,7 +35,7 @@ async def async_setup_entry(
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
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
],
True,
Expand All @@ -48,6 +50,7 @@ class LutronCover(LutronDevice, CoverEntity):
| CoverEntityFeature.CLOSE
| CoverEntityFeature.SET_POSITION
)
_lutron_device: Output

@property
def is_closed(self) -> bool:
Expand Down
13 changes: 10 additions & 3 deletions homeassistant/components/lutron/entity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Base class for Lutron devices."""

from pylutron import Lutron, LutronEntity, LutronEvent

from homeassistant.helpers.entity import Entity


Expand All @@ -7,7 +10,9 @@ class LutronDevice(Entity):

_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."""
self._lutron_device = lutron_device
self._controller = controller
Expand All @@ -17,7 +22,9 @@ 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):
def _update_callback(
self, _device: LutronEntity, _context: None, _event: LutronEvent, _params: dict
) -> None:
"""Run when invoked by pylutron when the device state changes."""
self.schedule_update_ha_state()

Expand All @@ -27,7 +34,7 @@ def name(self) -> str:
return f"{self._area_name} {self._lutron_device.name}"

@property
def unique_id(self):
def unique_id(self) -> str | None:
"""Return a unique ID."""
# Temporary fix for https://github.com/thecynic/pylutron/issues/70
if self._lutron_device.uuid is None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/lutron/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections.abc import Mapping
from typing import Any

from pylutron import Output

from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -48,11 +50,8 @@ class LutronLight(LutronDevice, LightEntity):

_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}

def __init__(self, area_name, lutron_device, controller) -> None:
"""Initialize the light."""
self._prev_brightness = None
super().__init__(area_name, lutron_device, controller)
_lutron_device: Output
_prev_brightness: int | None = None

@property
def brightness(self) -> int:
Expand Down
13 changes: 12 additions & 1 deletion homeassistant/components/lutron/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from typing import Any

from pylutron import Button, Led, Lutron

from homeassistant.components.scene import Scene
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -36,7 +38,16 @@ async def async_setup_entry(
class LutronScene(LutronDevice, 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."""
super().__init__(area_name, lutron_device, controller)
self._keypad_name = keypad_name
Expand Down
19 changes: 17 additions & 2 deletions homeassistant/components/lutron/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections.abc import Mapping
from typing import Any

from pylutron import Button, Led, Lutron, Output

from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -42,7 +44,11 @@ async def async_setup_entry(
class LutronSwitch(LutronDevice, SwitchEntity):
"""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."""
self._prev_state = None
super().__init__(area_name, lutron_device, controller)
Expand Down Expand Up @@ -74,7 +80,16 @@ def update(self) -> None:
class LutronLed(LutronDevice, SwitchEntity):
"""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."""
self._keypad_name = keypad_name
self._scene_name = scene_device.name
Expand Down

0 comments on commit 15ce706

Please sign in to comment.