Use attr** in lightwave (#61881)

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/62129/head
epenet 2021-12-16 22:57:15 +01:00 committed by GitHub
parent bb538a9782
commit 6778e4058e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 139 deletions

View File

@ -32,57 +32,46 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class LightwaveTrv(ClimateEntity):
"""Representation of a LightWaveRF TRV."""
_attr_hvac_mode = HVAC_MODE_HEAT
_attr_hvac_modes = [HVAC_MODE_HEAT, HVAC_MODE_OFF]
_attr_min_temp = DEFAULT_MIN_TEMP
_attr_max_temp = DEFAULT_MAX_TEMP
_attr_supported_features = SUPPORT_TARGET_TEMPERATURE
_attr_target_temperature_step = 0.5
_attr_temperature_unit = TEMP_CELSIUS
def __init__(self, name, device_id, lwlink, serial):
"""Initialize LightwaveTrv entity."""
self._name = name
self._attr_name = name
self._device_id = device_id
self._state = None
self._current_temperature = None
self._target_temperature = None
self._hvac_action = None
self._lwlink = lwlink
self._serial = serial
self._attr_unique_id = f"{serial}-trv"
# inhibit is used to prevent race condition on update. If non zero, skip next update cycle.
self._inhibit = 0
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_TARGET_TEMPERATURE
def update(self):
"""Communicate with a Lightwave RTF Proxy to get state."""
(temp, targ, _, trv_output) = self._lwlink.read_trv_status(self._serial)
if temp is not None:
self._current_temperature = temp
self._attr_current_temperature = temp
if targ is not None:
if self._inhibit == 0:
self._target_temperature = targ
self._attr_target_temperature = targ
if targ == 0:
# TRV off
self._target_temperature = None
self._attr_target_temperature = None
if targ >= 40:
# Call for heat mode, or TRV in a fixed position
self._target_temperature = None
self._attr_target_temperature = None
else:
# Done the job - use proxy next iteration
self._inhibit = 0
if trv_output is not None:
if trv_output > 0:
self._hvac_action = CURRENT_HVAC_HEAT
self._attr_hvac_action = CURRENT_HVAC_HEAT
else:
self._hvac_action = CURRENT_HVAC_OFF
@property
def name(self):
"""Lightwave trv name."""
return self._name
@property
def current_temperature(self):
"""Property giving the current room temperature."""
return self._current_temperature
self._attr_hvac_action = CURRENT_HVAC_OFF
@property
def target_temperature(self):
@ -92,51 +81,16 @@ class LightwaveTrv(ClimateEntity):
# propagated, the target temp is set back to the
# old target on the next poll, showing a false
# reading temporarily.
self._target_temperature = self._inhibit
return self._target_temperature
@property
def hvac_modes(self):
"""HVAC modes."""
return [HVAC_MODE_HEAT, HVAC_MODE_OFF]
@property
def hvac_mode(self):
"""HVAC mode."""
return HVAC_MODE_HEAT
@property
def hvac_action(self):
"""HVAC action."""
return self._hvac_action
@property
def min_temp(self):
"""Min Temp."""
return DEFAULT_MIN_TEMP
@property
def max_temp(self):
"""Max Temp."""
return DEFAULT_MAX_TEMP
@property
def temperature_unit(self):
"""Set temperature unit."""
return TEMP_CELSIUS
@property
def target_temperature_step(self):
"""Set temperature step."""
return 0.5
self._attr_target_temperature = self._inhibit
return self._attr_target_temperature
def set_temperature(self, **kwargs):
"""Set TRV target temperature."""
if ATTR_TEMPERATURE in kwargs:
self._target_temperature = kwargs[ATTR_TEMPERATURE]
self._inhibit = self._target_temperature
self._attr_target_temperature = kwargs[ATTR_TEMPERATURE]
self._inhibit = self._attr_target_temperature
self._lwlink.set_temperature(
self._device_id, self._target_temperature, self._name
self._device_id, self._attr_target_temperature, self._attr_name
)
async def async_set_hvac_mode(self, hvac_mode):

View File

@ -29,57 +29,34 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class LWRFLight(LightEntity):
"""Representation of a LightWaveRF light."""
_attr_supported_features = SUPPORT_BRIGHTNESS
_attr_should_poll = False
def __init__(self, name, device_id, lwlink):
"""Initialize LWRFLight entity."""
self._name = name
self._attr_name = name
self._device_id = device_id
self._state = None
self._brightness = MAX_BRIGHTNESS
self._attr_brightness = MAX_BRIGHTNESS
self._lwlink = lwlink
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
@property
def should_poll(self):
"""No polling needed for a LightWave light."""
return False
@property
def name(self):
"""Lightwave light name."""
return self._name
@property
def brightness(self):
"""Brightness of this light between 0..MAX_BRIGHTNESS."""
return self._brightness
@property
def is_on(self):
"""Lightwave light is on state."""
return self._state
async def async_turn_on(self, **kwargs):
"""Turn the LightWave light on."""
self._state = True
self._attr_is_on = True
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
if self._brightness != MAX_BRIGHTNESS:
if self._attr_brightness != MAX_BRIGHTNESS:
self._lwlink.turn_on_with_brightness(
self._device_id, self._name, self._brightness
self._device_id, self._attr_name, self._attr_brightness
)
else:
self._lwlink.turn_on_light(self._device_id, self._name)
self._lwlink.turn_on_light(self._device_id, self._attr_name)
self.async_write_ha_state()
async def async_turn_off(self, **kwargs):
"""Turn the LightWave light off."""
self._state = False
self._lwlink.turn_off(self._device_id, self._name)
self._attr_is_on = False
self._lwlink.turn_off(self._device_id, self._attr_name)
self.async_write_ha_state()

View File

@ -1,6 +1,10 @@
"""Support for LightwaveRF TRV - Associated Battery."""
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.const import CONF_NAME, DEVICE_CLASS_BATTERY, PERCENTAGE
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import CONF_NAME, PERCENTAGE
from . import CONF_SERIAL, LIGHTWAVE_LINK
@ -25,31 +29,20 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class LightwaveBattery(SensorEntity):
"""Lightwave TRV Battery."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_device_class = SensorDeviceClass.BATTERY
_attr_native_unit_of_measurement = PERCENTAGE
_attr_state_class = STATE_CLASS_MEASUREMENT
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(self, name, lwlink, serial):
"""Initialize the Lightwave Trv battery sensor."""
self._name = name
self._state = None
self._attr_name = name
self._lwlink = lwlink
self._serial = serial
self._attr_unique_id = f"{serial}-trv-battery"
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
def update(self):
"""Communicate with a Lightwave RTF Proxy to get state."""
(dummy_temp, dummy_targ, battery, dummy_output) = self._lwlink.read_trv_status(
self._serial
)
self._state = battery
self._attr_native_value = battery

View File

@ -23,36 +23,22 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class LWRFSwitch(SwitchEntity):
"""Representation of a LightWaveRF switch."""
_attr_should_poll = False
def __init__(self, name, device_id, lwlink):
"""Initialize LWRFSwitch entity."""
self._name = name
self._attr_name = name
self._device_id = device_id
self._state = None
self._lwlink = lwlink
@property
def should_poll(self):
"""No polling needed for a LightWave light."""
return False
@property
def name(self):
"""Lightwave switch name."""
return self._name
@property
def is_on(self):
"""Lightwave switch is on state."""
return self._state
async def async_turn_on(self, **kwargs):
"""Turn the LightWave switch on."""
self._state = True
self._lwlink.turn_on_switch(self._device_id, self._name)
self._attr_is_on = True
self._lwlink.turn_on_switch(self._device_id, self._attr_name)
self.async_write_ha_state()
async def async_turn_off(self, **kwargs):
"""Turn the LightWave switch off."""
self._state = False
self._lwlink.turn_off(self._device_id, self._name)
self._attr_is_on = False
self._lwlink.turn_off(self._device_id, self._attr_name)
self.async_write_ha_state()