Use entity class attributes for bosch_shc (#53057)

This commit is contained in:
Robert Hillis 2021-07-18 16:10:42 -04:00 committed by GitHub
parent 284e13464d
commit cb1eab6c24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 215 deletions

View File

@ -1,5 +1,6 @@
"""Platform for binarysensor integration.""" """Platform for binarysensor integration."""
from boschshcpy import SHCBatteryDevice, SHCSession, SHCShutterContact from boschshcpy import SHCBatteryDevice, SHCSession, SHCShutterContact
from boschshcpy.device import SHCDevice
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY, DEVICE_CLASS_BATTERY,
@ -50,37 +51,37 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class ShutterContactSensor(SHCEntity, BinarySensorEntity): class ShutterContactSensor(SHCEntity, BinarySensorEntity):
"""Representation of a SHC shutter contact sensor.""" """Representation of an SHC shutter contact sensor."""
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def is_on(self): """Initialize an SHC shutter contact sensor.."""
"""Return the state of the sensor.""" super().__init__(device, parent_id, entry_id)
return self._device.state == SHCShutterContact.ShutterContactService.State.OPEN
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
switcher = { switcher = {
"ENTRANCE_DOOR": DEVICE_CLASS_DOOR, "ENTRANCE_DOOR": DEVICE_CLASS_DOOR,
"REGULAR_WINDOW": DEVICE_CLASS_WINDOW, "REGULAR_WINDOW": DEVICE_CLASS_WINDOW,
"FRENCH_WINDOW": DEVICE_CLASS_DOOR, "FRENCH_WINDOW": DEVICE_CLASS_DOOR,
"GENERIC": DEVICE_CLASS_WINDOW, "GENERIC": DEVICE_CLASS_WINDOW,
} }
return switcher.get(self._device.device_class, DEVICE_CLASS_WINDOW) self._attr_device_class = switcher.get(
self._device.device_class, DEVICE_CLASS_WINDOW
)
@property
def is_on(self):
"""Return the state of the sensor."""
return self._device.state == SHCShutterContact.ShutterContactService.State.OPEN
class BatterySensor(SHCEntity, BinarySensorEntity): class BatterySensor(SHCEntity, BinarySensorEntity):
"""Representation of a SHC battery reporting sensor.""" """Representation of an SHC battery reporting sensor."""
@property _attr_device_class = DEVICE_CLASS_BATTERY
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_battery"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC battery reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Battery" self._attr_name = f"{device.name} Battery"
self._attr_unique_id = f"{device.serial}_battery"
@property @property
def is_on(self): def is_on(self):
@ -88,8 +89,3 @@ class BatterySensor(SHCEntity, BinarySensorEntity):
return ( return (
self._device.batterylevel != SHCBatteryDevice.BatteryLevelService.State.OK self._device.batterylevel != SHCBatteryDevice.BatteryLevelService.State.OK
) )
@property
def device_class(self):
"""Return the class of the sensor."""
return DEVICE_CLASS_BATTERY

View File

@ -20,11 +20,26 @@ async def async_remove_devices(hass, entity, entry_id):
class SHCEntity(Entity): class SHCEntity(Entity):
"""Representation of a SHC base entity.""" """Representation of a SHC base entity."""
_attr_should_poll = False
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None: def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize the generic SHC device.""" """Initialize the generic SHC device."""
self._device = device self._device = device
self._parent_id = parent_id
self._entry_id = entry_id self._entry_id = entry_id
self._attr_name = device.name
self._attr_unique_id = device.serial
self._attr_device_info = {
"identifiers": {(DOMAIN, device.id)},
"name": device.name,
"manufacturer": device.manufacturer,
"model": device.device_model,
"via_device": (
DOMAIN,
device.parent_device_id
if device.parent_device_id is not None
else parent_id,
),
}
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Subscribe to SHC events.""" """Subscribe to SHC events."""
@ -50,43 +65,7 @@ class SHCEntity(Entity):
service.unsubscribe_callback(self.entity_id) service.unsubscribe_callback(self.entity_id)
self._device.unsubscribe_callback(self.entity_id) self._device.unsubscribe_callback(self.entity_id)
@property
def unique_id(self):
"""Return the unique ID of the device."""
return self._device.serial
@property
def name(self):
"""Name of the entity."""
return self._device.name
@property
def device_id(self):
"""Device id of the entity."""
return self._device.id
@property
def device_info(self):
"""Return the device info."""
return {
"identifiers": {(DOMAIN, self.device_id)},
"name": self._device.name,
"manufacturer": self._device.manufacturer,
"model": self._device.device_model,
"via_device": (
DOMAIN,
self._device.parent_device_id
if self._device.parent_device_id is not None
else self._parent_id,
),
}
@property @property
def available(self): def available(self):
"""Return false if status is unavailable.""" """Return false if status is unavailable."""
return self._device.status == "AVAILABLE" return self._device.status == "AVAILABLE"
@property
def should_poll(self):
"""Report polling mode. SHC Entity is communicating via long polling."""
return False

View File

@ -1,5 +1,6 @@
"""Platform for sensor integration.""" """Platform for sensor integration."""
from boschshcpy import SHCSession from boschshcpy import SHCSession
from boschshcpy.device import SHCDevice
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ( from homeassistant.const import (
@ -143,104 +144,67 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TemperatureSensor(SHCEntity, SensorEntity): class TemperatureSensor(SHCEntity, SensorEntity):
"""Representation of a SHC temperature reporting sensor.""" """Representation of an SHC temperature reporting sensor."""
@property _attr_device_class = DEVICE_CLASS_TEMPERATURE
def unique_id(self): _attr_unit_of_measurement = TEMP_CELSIUS
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_temperature"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC temperature reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Temperature" self._attr_name = f"{device.name} Temperature"
self._attr_unique_id = f"{device.serial}_temperature"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.temperature return self._device.temperature
@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return TEMP_CELSIUS
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_TEMPERATURE
class HumiditySensor(SHCEntity, SensorEntity): class HumiditySensor(SHCEntity, SensorEntity):
"""Representation of a SHC humidity reporting sensor.""" """Representation of an SHC humidity reporting sensor."""
@property _attr_device_class = DEVICE_CLASS_HUMIDITY
def unique_id(self): _attr_unit_of_measurement = PERCENTAGE
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_humidity"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC humidity reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Humidity" self._attr_name = f"{device.name} Humidity"
self._attr_unique_id = f"{device.serial}_humidity"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.humidity return self._device.humidity
@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return PERCENTAGE
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_HUMIDITY
class PuritySensor(SHCEntity, SensorEntity): class PuritySensor(SHCEntity, SensorEntity):
"""Representation of a SHC purity reporting sensor.""" """Representation of an SHC purity reporting sensor."""
@property _attr_icon = "mdi:molecule-co2"
def unique_id(self): _attr_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_purity"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC purity reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Purity" self._attr_name = f"{device.name} Purity"
self._attr_unique_id = f"{device.serial}_purity"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.purity return self._device.purity
@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return CONCENTRATION_PARTS_PER_MILLION
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:molecule-co2"
class AirQualitySensor(SHCEntity, SensorEntity): class AirQualitySensor(SHCEntity, SensorEntity):
"""Representation of a SHC airquality reporting sensor.""" """Representation of an SHC airquality reporting sensor."""
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def unique_id(self): """Initialize an SHC airquality reporting sensor."""
"""Return the unique ID of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.serial}_airquality" self._attr_name = f"{device.name} Air Quality"
self._attr_unique_id = f"{device.serial}_airquality"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Air Quality"
@property @property
def state(self): def state(self):
@ -256,17 +220,13 @@ class AirQualitySensor(SHCEntity, SensorEntity):
class TemperatureRatingSensor(SHCEntity, SensorEntity): class TemperatureRatingSensor(SHCEntity, SensorEntity):
"""Representation of a SHC temperature rating sensor.""" """Representation of an SHC temperature rating sensor."""
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def unique_id(self): """Initialize an SHC temperature rating sensor."""
"""Return the unique ID of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.serial}_temperature_rating" self._attr_name = f"{device.name} Temperature Rating"
self._attr_unique_id = f"{device.serial}_temperature_rating"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Temperature Rating"
@property @property
def state(self): def state(self):
@ -275,17 +235,13 @@ class TemperatureRatingSensor(SHCEntity, SensorEntity):
class HumidityRatingSensor(SHCEntity, SensorEntity): class HumidityRatingSensor(SHCEntity, SensorEntity):
"""Representation of a SHC humidity rating sensor.""" """Representation of an SHC humidity rating sensor."""
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def unique_id(self): """Initialize an SHC humidity rating sensor."""
"""Return the unique ID of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.serial}_humidity_rating" self._attr_name = f"{device.name} Humidity Rating"
self._attr_unique_id = f"{device.serial}_humidity_rating"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Humidity Rating"
@property @property
def state(self): def state(self):
@ -294,17 +250,13 @@ class HumidityRatingSensor(SHCEntity, SensorEntity):
class PurityRatingSensor(SHCEntity, SensorEntity): class PurityRatingSensor(SHCEntity, SensorEntity):
"""Representation of a SHC purity rating sensor.""" """Representation of an SHC purity rating sensor."""
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def unique_id(self): """Initialize an SHC purity rating sensor."""
"""Return the unique ID of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.serial}_purity_rating" self._attr_name = f"{device.name} Purity Rating"
self._attr_unique_id = f"{device.serial}_purity_rating"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Purity Rating"
@property @property
def state(self): def state(self):
@ -313,91 +265,58 @@ class PurityRatingSensor(SHCEntity, SensorEntity):
class PowerSensor(SHCEntity, SensorEntity): class PowerSensor(SHCEntity, SensorEntity):
"""Representation of a SHC power reporting sensor.""" """Representation of an SHC power reporting sensor."""
@property _attr_device_class = DEVICE_CLASS_POWER
def unique_id(self): _attr_unit_of_measurement = POWER_WATT
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_power"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC power reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Power" self._attr_name = f"{device.name} Power"
self._attr_unique_id = f"{device.serial}_power"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.powerconsumption return self._device.powerconsumption
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_POWER
@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return POWER_WATT
class EnergySensor(SHCEntity, SensorEntity): class EnergySensor(SHCEntity, SensorEntity):
"""Representation of a SHC energy reporting sensor.""" """Representation of an SHC energy reporting sensor."""
@property _attr_device_class = DEVICE_CLASS_ENERGY
def unique_id(self): _attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_energy"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC energy reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Energy" self._attr_name = f"{self._device.name} Energy"
self._attr_unique_id = f"{self._device.serial}_energy"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.energyconsumption / 1000.0 return self._device.energyconsumption / 1000.0
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_ENERGY
@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return ENERGY_KILO_WATT_HOUR
class ValveTappetSensor(SHCEntity, SensorEntity): class ValveTappetSensor(SHCEntity, SensorEntity):
"""Representation of a SHC valve tappet reporting sensor.""" """Representation of an SHC valve tappet reporting sensor."""
@property _attr_icon = "mdi:gauge"
def unique_id(self): _attr_unit_of_measurement = PERCENTAGE
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_valvetappet"
@property def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
def name(self): """Initialize an SHC valve tappet reporting sensor."""
"""Return the name of this sensor.""" super().__init__(device, parent_id, entry_id)
return f"{self._device.name} Valvetappet" self._attr_name = f"{device.name} Valvetappet"
self._attr_unique_id = f"{device.serial}_valvetappet"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._device.position return self._device.position
@property
def icon(self):
"""Return the icon of the sensor."""
return "mdi:gauge"
@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return PERCENTAGE
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""