Use attr** in smarty (#62371)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-20 18:42:36 +01:00 committed by GitHub
parent ba818c0a95
commit c04e181809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 83 deletions

View File

@ -3,7 +3,7 @@
import logging import logging
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_PROBLEM, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.core import callback from homeassistant.core import callback
@ -31,33 +31,14 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SmartyBinarySensor(BinarySensorEntity): class SmartyBinarySensor(BinarySensorEntity):
"""Representation of a Smarty Binary Sensor.""" """Representation of a Smarty Binary Sensor."""
_attr_should_poll = False
def __init__(self, name, device_class, smarty): def __init__(self, name, device_class, smarty):
"""Initialize the entity.""" """Initialize the entity."""
self._name = name self._attr_name = name
self._state = None self._attr_device_class = device_class
self._sensor_type = device_class
self._smarty = smarty self._smarty = smarty
@property
def device_class(self):
"""Return the class of the sensor."""
return self._sensor_type
@property
def should_poll(self) -> bool:
"""Do not poll."""
return False
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Call to update.""" """Call to update."""
async_dispatcher_connect(self.hass, SIGNAL_UPDATE_SMARTY, self._update_callback) async_dispatcher_connect(self.hass, SIGNAL_UPDATE_SMARTY, self._update_callback)
@ -77,8 +58,8 @@ class BoostSensor(SmartyBinarySensor):
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.boost self._attr_is_on = self._smarty.boost
class AlarmSensor(SmartyBinarySensor): class AlarmSensor(SmartyBinarySensor):
@ -87,13 +68,15 @@ class AlarmSensor(SmartyBinarySensor):
def __init__(self, name, smarty): def __init__(self, name, smarty):
"""Alarm Sensor Init.""" """Alarm Sensor Init."""
super().__init__( super().__init__(
name=f"{name} Alarm", device_class=DEVICE_CLASS_PROBLEM, smarty=smarty name=f"{name} Alarm",
device_class=BinarySensorDeviceClass.PROBLEM,
smarty=smarty,
) )
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.alarm self._attr_is_on = self._smarty.alarm
class WarningSensor(SmartyBinarySensor): class WarningSensor(SmartyBinarySensor):
@ -102,10 +85,12 @@ class WarningSensor(SmartyBinarySensor):
def __init__(self, name, smarty): def __init__(self, name, smarty):
"""Warning Sensor Init.""" """Warning Sensor Init."""
super().__init__( super().__init__(
name=f"{name} Warning", device_class=DEVICE_CLASS_PROBLEM, smarty=smarty name=f"{name} Warning",
device_class=BinarySensorDeviceClass.PROBLEM,
smarty=smarty,
) )
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.warning self._attr_is_on = self._smarty.warning

View File

@ -4,12 +4,8 @@ from __future__ import annotations
import datetime as dt import datetime as dt
import logging import logging
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import ( from homeassistant.const import TEMP_CELSIUS
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
TEMP_CELSIUS,
)
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -39,41 +35,18 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SmartySensor(SensorEntity): class SmartySensor(SensorEntity):
"""Representation of a Smarty Sensor.""" """Representation of a Smarty Sensor."""
_attr_should_poll = False
def __init__( def __init__(
self, name: str, device_class: str, smarty, unit_of_measurement: str = "" self, name: str, device_class: str, smarty, unit_of_measurement: str = ""
): ):
"""Initialize the entity.""" """Initialize the entity."""
self._name = name self._attr_name = name
self._state: dt.datetime | None = None self._attr_native_value = None
self._sensor_type = device_class self._attr_device_class = device_class
self._unit_of_measurement = unit_of_measurement self._attr_native_unit_of_measurement = unit_of_measurement
self._smarty = smarty self._smarty = smarty
@property
def should_poll(self) -> bool:
"""Do not poll."""
return False
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._sensor_type
@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
@property
def native_unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return self._unit_of_measurement
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Call to update.""" """Call to update."""
async_dispatcher_connect(self.hass, SIGNAL_UPDATE_SMARTY, self._update_callback) async_dispatcher_connect(self.hass, SIGNAL_UPDATE_SMARTY, self._update_callback)
@ -91,15 +64,15 @@ class SupplyAirTemperatureSensor(SmartySensor):
"""Supply Air Temperature Init.""" """Supply Air Temperature Init."""
super().__init__( super().__init__(
name=f"{name} Supply Air Temperature", name=f"{name} Supply Air Temperature",
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
smarty=smarty, smarty=smarty,
) )
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.supply_air_temperature self._attr_native_value = self._smarty.supply_air_temperature
class ExtractAirTemperatureSensor(SmartySensor): class ExtractAirTemperatureSensor(SmartySensor):
@ -109,15 +82,15 @@ class ExtractAirTemperatureSensor(SmartySensor):
"""Supply Air Temperature Init.""" """Supply Air Temperature Init."""
super().__init__( super().__init__(
name=f"{name} Extract Air Temperature", name=f"{name} Extract Air Temperature",
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
smarty=smarty, smarty=smarty,
) )
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.extract_air_temperature self._attr_native_value = self._smarty.extract_air_temperature
class OutdoorAirTemperatureSensor(SmartySensor): class OutdoorAirTemperatureSensor(SmartySensor):
@ -127,15 +100,15 @@ class OutdoorAirTemperatureSensor(SmartySensor):
"""Outdoor Air Temperature Init.""" """Outdoor Air Temperature Init."""
super().__init__( super().__init__(
name=f"{name} Outdoor Air Temperature", name=f"{name} Outdoor Air Temperature",
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
smarty=smarty, smarty=smarty,
) )
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.outdoor_air_temperature self._attr_native_value = self._smarty.outdoor_air_temperature
class SupplyFanSpeedSensor(SmartySensor): class SupplyFanSpeedSensor(SmartySensor):
@ -152,8 +125,8 @@ class SupplyFanSpeedSensor(SmartySensor):
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.supply_fan_speed self._attr_native_value = self._smarty.supply_fan_speed
class ExtractFanSpeedSensor(SmartySensor): class ExtractFanSpeedSensor(SmartySensor):
@ -170,8 +143,8 @@ class ExtractFanSpeedSensor(SmartySensor):
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
self._state = self._smarty.extract_fan_speed self._attr_native_value = self._smarty.extract_fan_speed
class FilterDaysLeftSensor(SmartySensor): class FilterDaysLeftSensor(SmartySensor):
@ -181,7 +154,7 @@ class FilterDaysLeftSensor(SmartySensor):
"""Filter Days Left Init.""" """Filter Days Left Init."""
super().__init__( super().__init__(
name=f"{name} Filter Days Left", name=f"{name} Filter Days Left",
device_class=DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
unit_of_measurement=None, unit_of_measurement=None,
smarty=smarty, smarty=smarty,
) )
@ -189,8 +162,8 @@ class FilterDaysLeftSensor(SmartySensor):
def update(self) -> None: def update(self) -> None:
"""Update state.""" """Update state."""
_LOGGER.debug("Updating sensor %s", self._name) _LOGGER.debug("Updating sensor %s", self._attr_name)
days_left = self._smarty.filter_timer days_left = self._smarty.filter_timer
if days_left is not None and days_left != self._days_left: if days_left is not None and days_left != self._days_left:
self._state = dt_util.now() + dt.timedelta(days=days_left) self._attr_native_value = dt_util.now() + dt.timedelta(days=days_left)
self._days_left = days_left self._days_left = days_left