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."""
from boschshcpy import SHCBatteryDevice, SHCSession, SHCShutterContact
from boschshcpy.device import SHCDevice
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY,
@ -50,37 +51,37 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class ShutterContactSensor(SHCEntity, BinarySensorEntity):
"""Representation of a SHC shutter contact sensor."""
"""Representation of an SHC shutter contact sensor."""
@property
def is_on(self):
"""Return the state of the sensor."""
return self._device.state == SHCShutterContact.ShutterContactService.State.OPEN
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC shutter contact sensor.."""
super().__init__(device, parent_id, entry_id)
switcher = {
"ENTRANCE_DOOR": DEVICE_CLASS_DOOR,
"REGULAR_WINDOW": DEVICE_CLASS_WINDOW,
"FRENCH_WINDOW": DEVICE_CLASS_DOOR,
"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):
"""Representation of a SHC battery reporting sensor."""
"""Representation of an SHC battery reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_battery"
_attr_device_class = DEVICE_CLASS_BATTERY
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Battery"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC battery reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Battery"
self._attr_unique_id = f"{device.serial}_battery"
@property
def is_on(self):
@ -88,8 +89,3 @@ class BatterySensor(SHCEntity, BinarySensorEntity):
return (
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):
"""Representation of a SHC base entity."""
_attr_should_poll = False
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize the generic SHC device."""
self._device = device
self._parent_id = parent_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):
"""Subscribe to SHC events."""
@ -50,43 +65,7 @@ class SHCEntity(Entity):
service.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
def available(self):
"""Return false if status is unavailable."""
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."""
from boschshcpy import SHCSession
from boschshcpy.device import SHCDevice
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import (
@ -143,104 +144,67 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TemperatureSensor(SHCEntity, SensorEntity):
"""Representation of a SHC temperature reporting sensor."""
"""Representation of an SHC temperature reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_temperature"
_attr_device_class = DEVICE_CLASS_TEMPERATURE
_attr_unit_of_measurement = TEMP_CELSIUS
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Temperature"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC temperature reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Temperature"
self._attr_unique_id = f"{device.serial}_temperature"
@property
def state(self):
"""Return the state of the sensor."""
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):
"""Representation of a SHC humidity reporting sensor."""
"""Representation of an SHC humidity reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_humidity"
_attr_device_class = DEVICE_CLASS_HUMIDITY
_attr_unit_of_measurement = PERCENTAGE
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Humidity"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC humidity reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Humidity"
self._attr_unique_id = f"{device.serial}_humidity"
@property
def state(self):
"""Return the state of the sensor."""
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):
"""Representation of a SHC purity reporting sensor."""
"""Representation of an SHC purity reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_purity"
_attr_icon = "mdi:molecule-co2"
_attr_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Purity"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC purity reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Purity"
self._attr_unique_id = f"{device.serial}_purity"
@property
def state(self):
"""Return the state of the sensor."""
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):
"""Representation of a SHC airquality reporting sensor."""
"""Representation of an SHC airquality reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_airquality"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Air Quality"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC airquality reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Air Quality"
self._attr_unique_id = f"{device.serial}_airquality"
@property
def state(self):
@ -256,17 +220,13 @@ class AirQualitySensor(SHCEntity, SensorEntity):
class TemperatureRatingSensor(SHCEntity, SensorEntity):
"""Representation of a SHC temperature rating sensor."""
"""Representation of an SHC temperature rating sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_temperature_rating"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Temperature Rating"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC temperature rating sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Temperature Rating"
self._attr_unique_id = f"{device.serial}_temperature_rating"
@property
def state(self):
@ -275,17 +235,13 @@ class TemperatureRatingSensor(SHCEntity, SensorEntity):
class HumidityRatingSensor(SHCEntity, SensorEntity):
"""Representation of a SHC humidity rating sensor."""
"""Representation of an SHC humidity rating sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_humidity_rating"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Humidity Rating"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC humidity rating sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Humidity Rating"
self._attr_unique_id = f"{device.serial}_humidity_rating"
@property
def state(self):
@ -294,17 +250,13 @@ class HumidityRatingSensor(SHCEntity, SensorEntity):
class PurityRatingSensor(SHCEntity, SensorEntity):
"""Representation of a SHC purity rating sensor."""
"""Representation of an SHC purity rating sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_purity_rating"
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Purity Rating"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC purity rating sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Purity Rating"
self._attr_unique_id = f"{device.serial}_purity_rating"
@property
def state(self):
@ -313,91 +265,58 @@ class PurityRatingSensor(SHCEntity, SensorEntity):
class PowerSensor(SHCEntity, SensorEntity):
"""Representation of a SHC power reporting sensor."""
"""Representation of an SHC power reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_power"
_attr_device_class = DEVICE_CLASS_POWER
_attr_unit_of_measurement = POWER_WATT
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Power"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC power reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Power"
self._attr_unique_id = f"{device.serial}_power"
@property
def state(self):
"""Return the state of the sensor."""
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):
"""Representation of a SHC energy reporting sensor."""
"""Representation of an SHC energy reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_energy"
_attr_device_class = DEVICE_CLASS_ENERGY
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Energy"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC energy reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{self._device.name} Energy"
self._attr_unique_id = f"{self._device.serial}_energy"
@property
def state(self):
"""Return the state of the sensor."""
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):
"""Representation of a SHC valve tappet reporting sensor."""
"""Representation of an SHC valve tappet reporting sensor."""
@property
def unique_id(self):
"""Return the unique ID of this sensor."""
return f"{self._device.serial}_valvetappet"
_attr_icon = "mdi:gauge"
_attr_unit_of_measurement = PERCENTAGE
@property
def name(self):
"""Return the name of this sensor."""
return f"{self._device.name} Valvetappet"
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC valve tappet reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Valvetappet"
self._attr_unique_id = f"{device.serial}_valvetappet"
@property
def state(self):
"""Return the state of the sensor."""
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
def extra_state_attributes(self):
"""Return the state attributes."""