Use shorthand attributes in Vera (#99893)

This commit is contained in:
Joost Lekkerkerker 2023-09-12 15:23:12 +02:00 committed by GitHub
parent 6b265120b3
commit e143bdf2f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 80 deletions

View file

@ -32,20 +32,16 @@ async def async_setup_entry(
class VeraBinarySensor(VeraDevice[veraApi.VeraBinarySensor], BinarySensorEntity):
"""Representation of a Vera Binary Sensor."""
_attr_is_on = False
def __init__(
self, vera_device: veraApi.VeraBinarySensor, controller_data: ControllerData
) -> None:
"""Initialize the binary_sensor."""
self._state = False
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
@property
def is_on(self) -> bool | None:
"""Return true if sensor is on."""
return self._state
def update(self) -> None:
"""Get the latest data and update the state."""
super().update()
self._state = self.vera_device.is_tripped
self._attr_is_on = self.vera_device.is_tripped

View file

@ -46,6 +46,7 @@ class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity):
"""Representation of a Vera Thermostat."""
_attr_hvac_modes = SUPPORT_HVAC
_attr_fan_modes = FAN_OPERATION_LIST
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
)
@ -79,11 +80,6 @@ class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity):
return FAN_ON
return FAN_AUTO
@property
def fan_modes(self) -> list[str] | None:
"""Return a list of available fan modes."""
return FAN_OPERATION_LIST
def set_fan_mode(self, fan_mode: str) -> None:
"""Set new target temperature."""
if fan_mode == FAN_ON:

View file

@ -41,31 +41,22 @@ async def async_setup_entry(
class VeraLight(VeraDevice[veraApi.VeraDimmer], LightEntity):
"""Representation of a Vera Light, including dimmable."""
_attr_is_on = False
_attr_hs_color: tuple[float, float] | None = None
_attr_brightness: int | None = None
def __init__(
self, vera_device: veraApi.VeraDimmer, controller_data: ControllerData
) -> None:
"""Initialize the light."""
self._state = False
self._color: tuple[float, float] | None = None
self._brightness = None
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
@property
def brightness(self) -> int | None:
"""Return the brightness of the light."""
return self._brightness
@property
def hs_color(self) -> tuple[float, float] | None:
"""Return the color of the light."""
return self._color
@property
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
if self.vera_device.is_dimmable:
if self._color:
if self._attr_hs_color:
return ColorMode.HS
return ColorMode.BRIGHTNESS
return ColorMode.ONOFF
@ -77,7 +68,7 @@ class VeraLight(VeraDevice[veraApi.VeraDimmer], LightEntity):
def turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
if ATTR_HS_COLOR in kwargs and self._color:
if ATTR_HS_COLOR in kwargs and self._attr_hs_color:
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
self.vera_device.set_color(rgb)
elif ATTR_BRIGHTNESS in kwargs and self.vera_device.is_dimmable:
@ -85,27 +76,22 @@ class VeraLight(VeraDevice[veraApi.VeraDimmer], LightEntity):
else:
self.vera_device.switch_on()
self._state = True
self._attr_is_on = True
self.schedule_update_ha_state(True)
def turn_off(self, **kwargs: Any) -> None:
"""Turn the light off."""
self.vera_device.switch_off()
self._state = False
self._attr_is_on = False
self.schedule_update_ha_state()
@property
def is_on(self) -> bool:
"""Return true if device is on."""
return self._state
def update(self) -> None:
"""Call to update state."""
super().update()
self._state = self.vera_device.is_switched_on()
self._attr_is_on = self.vera_device.is_switched_on()
if self.vera_device.is_dimmable:
# If it is dimmable, both functions exist. In case color
# is not supported, it will return None
self._brightness = self.vera_device.get_brightness()
self._attr_brightness = self.vera_device.get_brightness()
rgb = self.vera_device.get_color()
self._color = color_util.color_RGB_to_hs(*rgb) if rgb else None
self._attr_hs_color = color_util.color_RGB_to_hs(*rgb) if rgb else None

View file

@ -7,7 +7,7 @@ import pyvera as veraApi
from homeassistant.components.lock import ENTITY_ID_FORMAT, LockEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED, Platform
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -41,24 +41,18 @@ class VeraLock(VeraDevice[veraApi.VeraLock], LockEntity):
self, vera_device: veraApi.VeraLock, controller_data: ControllerData
) -> None:
"""Initialize the Vera device."""
self._state: str | None = None
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
def lock(self, **kwargs: Any) -> None:
"""Lock the device."""
self.vera_device.lock()
self._state = STATE_LOCKED
self._attr_is_locked = True
def unlock(self, **kwargs: Any) -> None:
"""Unlock the device."""
self.vera_device.unlock()
self._state = STATE_UNLOCKED
@property
def is_locked(self) -> bool | None:
"""Return true if device is on."""
return self._state == STATE_LOCKED
self._attr_is_locked = False
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
@ -91,6 +85,4 @@ class VeraLock(VeraDevice[veraApi.VeraLock], LockEntity):
def update(self) -> None:
"""Update state by the Vera device callback."""
self._state = (
STATE_LOCKED if self.vera_device.is_locked(True) else STATE_UNLOCKED
)
self._attr_is_locked = self.vera_device.is_locked(True)

View file

@ -37,7 +37,7 @@ class VeraScene(Scene):
self.vera_scene = vera_scene
self.controller = controller_data.controller
self._name = self.vera_scene.name
self._attr_name = self.vera_scene.name
# Append device id to prevent name clashes in HA.
self.vera_id = VERA_ID_FORMAT.format(
slugify(vera_scene.name), vera_scene.scene_id
@ -51,11 +51,6 @@ class VeraScene(Scene):
"""Activate the scene."""
self.vera_scene.activate()
@property
def name(self) -> str:
"""Return the name of the scene."""
return self._name
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes of the scene."""

View file

@ -21,7 +21,6 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import VeraDevice
from .common import ControllerData, get_controller_data
@ -52,17 +51,11 @@ class VeraSensor(VeraDevice[veraApi.VeraSensor], SensorEntity):
self, vera_device: veraApi.VeraSensor, controller_data: ControllerData
) -> None:
"""Initialize the sensor."""
self.current_value: StateType = None
self._temperature_units: str | None = None
self.last_changed_time = None
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
@property
def native_value(self) -> StateType:
"""Return the name of the sensor."""
return self.current_value
@property
def device_class(self) -> SensorDeviceClass | None:
"""Return the class of this entity."""
@ -96,7 +89,7 @@ class VeraSensor(VeraDevice[veraApi.VeraSensor], SensorEntity):
"""Update the state."""
super().update()
if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
self.current_value = self.vera_device.temperature
self._attr_native_value = self.vera_device.temperature
vera_temp_units = self.vera_device.vera_controller.temperature_units
@ -106,24 +99,24 @@ class VeraSensor(VeraDevice[veraApi.VeraSensor], SensorEntity):
self._temperature_units = UnitOfTemperature.CELSIUS
elif self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
self.current_value = self.vera_device.light
self._attr_native_value = self.vera_device.light
elif self.vera_device.category == veraApi.CATEGORY_UV_SENSOR:
self.current_value = self.vera_device.light
self._attr_native_value = self.vera_device.light
elif self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
self.current_value = self.vera_device.humidity
self._attr_native_value = self.vera_device.humidity
elif self.vera_device.category == veraApi.CATEGORY_SCENE_CONTROLLER:
controller = cast(veraApi.VeraSceneController, self.vera_device)
value = controller.get_last_scene_id(True)
time = controller.get_last_scene_time(True)
if time == self.last_changed_time:
self.current_value = None
self._attr_native_value = None
else:
self.current_value = value
self._attr_native_value = value
self.last_changed_time = time
elif self.vera_device.category == veraApi.CATEGORY_POWER_METER:
self.current_value = self.vera_device.power
self._attr_native_value = self.vera_device.power
elif self.vera_device.is_trippable:
tripped = self.vera_device.is_tripped
self.current_value = "Tripped" if tripped else "Not Tripped"
self._attr_native_value = "Tripped" if tripped else "Not Tripped"
else:
self.current_value = "Unknown"
self._attr_native_value = "Unknown"

View file

@ -34,32 +34,28 @@ async def async_setup_entry(
class VeraSwitch(VeraDevice[veraApi.VeraSwitch], SwitchEntity):
"""Representation of a Vera Switch."""
_attr_is_on = False
def __init__(
self, vera_device: veraApi.VeraSwitch, controller_data: ControllerData
) -> None:
"""Initialize the Vera device."""
self._state = False
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
def turn_on(self, **kwargs: Any) -> None:
"""Turn device on."""
self.vera_device.switch_on()
self._state = True
self._attr_is_on = True
self.schedule_update_ha_state()
def turn_off(self, **kwargs: Any) -> None:
"""Turn device off."""
self.vera_device.switch_off()
self._state = False
self._attr_is_on = False
self.schedule_update_ha_state()
@property
def is_on(self) -> bool:
"""Return true if device is on."""
return self._state
def update(self) -> None:
"""Update device state."""
super().update()
self._state = self.vera_device.is_switched_on()
self._attr_is_on = self.vera_device.is_switched_on()