Add lutron fan entity (#107402)

* add support for fan entity

* removed unused variables

* removed preset leftovers - not needed

* added deprecation for fans

* Update __init__.py

* fix typing

* initial updates based on review

* updated to search on unique ID instead of entity ID.

* updates for nits

* nits updates

* updates for new callback

* removed async per nits

* wrapped comments into shorter lines

* Add comment comma

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
wilburCforce 2024-01-29 13:58:12 -06:00 committed by GitHub
parent a289ab9044
commit 39d263599e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 190 additions and 4 deletions

View file

@ -2,18 +2,30 @@
from __future__ import annotations
from collections.abc import Mapping
import logging
from typing import Any
from pylutron import Output
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.components.script import scripts_with_entity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import (
IssueSeverity,
async_create_issue,
create_issue,
)
from . import DOMAIN, LutronData
from .entity import LutronDevice
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
@ -25,12 +37,50 @@ async def async_setup_entry(
Adds dimmers from the Main Repeater associated with the config_entry as
light entities.
"""
ent_reg = er.async_get(hass)
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
lights = []
for area_name, device in entry_data.lights:
if device.type == "CEILING_FAN_TYPE2":
# If this is a fan, check to see if this entity already exists.
# If not, do not create a new one.
entity_id = ent_reg.async_get_entity_id(
Platform.LIGHT,
DOMAIN,
f"{entry_data.client.guid}_{device.uuid}",
)
if entity_id:
entity_entry = ent_reg.async_get(entity_id)
assert entity_entry
if entity_entry.disabled:
# If the entity exists and is disabled then we want to remove
# the entity so that the user is using the new fan entity instead.
ent_reg.async_remove(entity_id)
else:
lights.append(LutronLight(area_name, device, entry_data.client))
entity_automations = automations_with_entity(hass, entity_id)
entity_scripts = scripts_with_entity(hass, entity_id)
for item in entity_automations + entity_scripts:
async_create_issue(
hass,
DOMAIN,
f"deprecated_light_fan_{entity_id}_{item}",
breaks_in_ha_version="2024.8.0",
is_fixable=True,
is_persistent=True,
severity=IssueSeverity.WARNING,
translation_key="deprecated_light_fan_entity",
translation_placeholders={
"entity": entity_id,
"info": item,
},
)
else:
lights.append(LutronLight(area_name, device, entry_data.client))
async_add_entities(
[
LutronLight(area_name, device, entry_data.client)
for area_name, device in entry_data.lights
],
lights,
True,
)
@ -54,8 +104,24 @@ class LutronLight(LutronDevice, LightEntity):
_prev_brightness: int | None = None
_attr_name = None
def __init__(self, area_name, lutron_device, controller) -> None:
"""Initialize the light."""
super().__init__(area_name, lutron_device, controller)
self._is_fan = lutron_device.type == "CEILING_FAN_TYPE"
def turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
if self._is_fan:
create_issue(
self.hass,
DOMAIN,
"deprecated_light_fan_on",
breaks_in_ha_version="2024.8.0",
is_fixable=True,
is_persistent=True,
severity=IssueSeverity.WARNING,
translation_key="deprecated_light_fan_on",
)
if ATTR_BRIGHTNESS in kwargs and self._lutron_device.is_dimmable:
brightness = kwargs[ATTR_BRIGHTNESS]
elif self._prev_brightness == 0:
@ -67,6 +133,17 @@ class LutronLight(LutronDevice, LightEntity):
def turn_off(self, **kwargs: Any) -> None:
"""Turn the light off."""
if self._is_fan:
create_issue(
self.hass,
DOMAIN,
"deprecated_light_fan_off",
breaks_in_ha_version="2024.8.0",
is_fixable=True,
is_persistent=True,
severity=IssueSeverity.WARNING,
translation_key="deprecated_light_fan_off",
)
self._lutron_device.level = 0
@property