Use shorthand attributes in Opentherm gateway (#99630)

pull/99859/head
Joost Lekkerkerker 2023-09-07 18:30:58 +02:00 committed by GitHub
parent c567a2c3d4
commit 94aec3e590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 151 deletions

View File

@ -52,6 +52,7 @@ class OpenThermBinarySensor(BinarySensorEntity):
"""Represent an OpenTherm Gateway binary sensor."""
_attr_should_poll = False
_attr_entity_registry_enabled_default = False
def __init__(self, gw_dev, var, source, device_class, friendly_name_format):
"""Initialize the binary sensor."""
@ -61,73 +62,42 @@ class OpenThermBinarySensor(BinarySensorEntity):
self._gateway = gw_dev
self._var = var
self._source = source
self._state = None
self._device_class = device_class
self._attr_device_class = device_class
if TRANSLATE_SOURCE[source] is not None:
friendly_name_format = (
f"{friendly_name_format} ({TRANSLATE_SOURCE[source]})"
)
self._friendly_name = friendly_name_format.format(gw_dev.name)
self._attr_name = friendly_name_format.format(gw_dev.name)
self._unsub_updates = None
self._attr_unique_id = f"{gw_dev.gw_id}-{source}-{var}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, gw_dev.gw_id)},
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
name=gw_dev.name,
sw_version=gw_dev.gw_version,
)
async def async_added_to_hass(self) -> None:
"""Subscribe to updates from the component."""
_LOGGER.debug("Added OpenTherm Gateway binary sensor %s", self._friendly_name)
_LOGGER.debug("Added OpenTherm Gateway binary sensor %s", self._attr_name)
self._unsub_updates = async_dispatcher_connect(
self.hass, self._gateway.update_signal, self.receive_report
)
async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from updates from the component."""
_LOGGER.debug(
"Removing OpenTherm Gateway binary sensor %s", self._friendly_name
)
_LOGGER.debug("Removing OpenTherm Gateway binary sensor %s", self._attr_name)
self._unsub_updates()
@property
def available(self):
"""Return availability of the sensor."""
return self._state is not None
@property
def entity_registry_enabled_default(self):
"""Disable binary_sensors by default."""
return False
return self._attr_is_on is not None
@callback
def receive_report(self, status):
"""Handle status updates from the component."""
state = status[self._source].get(self._var)
self._state = None if state is None else bool(state)
self._attr_is_on = None if state is None else bool(state)
self.async_write_ha_state()
@property
def name(self):
"""Return the friendly name."""
return self._friendly_name
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._gateway.gw_id)},
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
name=self._gateway.name,
sw_version=self._gateway.gw_version,
)
@property
def unique_id(self):
"""Return a unique ID."""
return f"{self._gateway.gw_id}-{self._source}-{self._var}"
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
@property
def device_class(self):
"""Return the class of this device."""
return self._device_class

View File

@ -70,6 +70,20 @@ class OpenThermClimate(ClimateEntity):
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
)
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_available = False
_attr_hvac_modes = []
_attr_preset_modes = []
_attr_min_temp = 1
_attr_max_temp = 30
_hvac_mode = HVACMode.HEAT
_current_temperature: float | None = None
_new_target_temperature: float | None = None
_target_temperature: float | None = None
_away_mode_a: int | None = None
_away_mode_b: int | None = None
_away_state_a = False
_away_state_b = False
_current_operation: HVACAction | None = None
def __init__(self, gw_dev, options):
"""Initialize the device."""
@ -78,22 +92,21 @@ class OpenThermClimate(ClimateEntity):
ENTITY_ID_FORMAT, gw_dev.gw_id, hass=gw_dev.hass
)
self.friendly_name = gw_dev.name
self._attr_name = self.friendly_name
self.floor_temp = options.get(CONF_FLOOR_TEMP, DEFAULT_FLOOR_TEMP)
self.temp_read_precision = options.get(CONF_READ_PRECISION)
self.temp_set_precision = options.get(CONF_SET_PRECISION)
self.temporary_ovrd_mode = options.get(CONF_TEMPORARY_OVRD_MODE, True)
self._available = False
self._current_operation: HVACAction | None = None
self._current_temperature = None
self._hvac_mode = HVACMode.HEAT
self._new_target_temperature = None
self._target_temperature = None
self._away_mode_a = None
self._away_mode_b = None
self._away_state_a = False
self._away_state_b = False
self._unsub_options = None
self._unsub_updates = None
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, gw_dev.gw_id)},
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
name=gw_dev.name,
sw_version=gw_dev.gw_version,
)
self._attr_unique_id = gw_dev.gw_id
@callback
def update_options(self, entry):
@ -123,7 +136,7 @@ class OpenThermClimate(ClimateEntity):
@callback
def receive_report(self, status):
"""Receive and handle a new report from the Gateway."""
self._available = status != gw_vars.DEFAULT_STATUS
self._attr_available = status != gw_vars.DEFAULT_STATUS
ch_active = status[gw_vars.BOILER].get(gw_vars.DATA_SLAVE_CH_ACTIVE)
flame_on = status[gw_vars.BOILER].get(gw_vars.DATA_SLAVE_FLAME_ON)
cooling_active = status[gw_vars.BOILER].get(gw_vars.DATA_SLAVE_COOLING_ACTIVE)
@ -171,32 +184,6 @@ class OpenThermClimate(ClimateEntity):
)
self.async_write_ha_state()
@property
def available(self):
"""Return availability of the sensor."""
return self._available
@property
def name(self):
"""Return the friendly name."""
return self.friendly_name
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._gateway.gw_id)},
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
name=self._gateway.name,
sw_version=self._gateway.gw_version,
)
@property
def unique_id(self):
"""Return a unique ID."""
return self._gateway.gw_id
@property
def precision(self):
"""Return the precision of the system."""
@ -216,11 +203,6 @@ class OpenThermClimate(ClimateEntity):
"""Return current HVAC mode."""
return self._hvac_mode
@property
def hvac_modes(self) -> list[HVACMode]:
"""Return available HVAC modes."""
return []
def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set the HVAC mode."""
_LOGGER.warning("Changing HVAC mode is not supported")
@ -259,11 +241,6 @@ class OpenThermClimate(ClimateEntity):
return PRESET_AWAY
return PRESET_NONE
@property
def preset_modes(self):
"""Available preset modes to set."""
return []
def set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode."""
_LOGGER.warning("Changing preset mode is not supported")
@ -278,13 +255,3 @@ class OpenThermClimate(ClimateEntity):
temp, self.temporary_ovrd_mode
)
self.async_write_ha_state()
@property
def min_temp(self):
"""Return the minimum temperature."""
return 1
@property
def max_temp(self):
"""Return the maximum temperature."""
return 30

View File

@ -49,6 +49,7 @@ class OpenThermSensor(SensorEntity):
"""Representation of an OpenTherm Gateway sensor."""
_attr_should_poll = False
_attr_entity_registry_enabled_default = False
def __init__(self, gw_dev, var, source, device_class, unit, friendly_name_format):
"""Initialize the OpenTherm Gateway sensor."""
@ -58,37 +59,39 @@ class OpenThermSensor(SensorEntity):
self._gateway = gw_dev
self._var = var
self._source = source
self._value = None
self._device_class = device_class
self._unit = unit
self._attr_device_class = device_class
self._attr_native_unit_of_measurement = unit
if TRANSLATE_SOURCE[source] is not None:
friendly_name_format = (
f"{friendly_name_format} ({TRANSLATE_SOURCE[source]})"
)
self._friendly_name = friendly_name_format.format(gw_dev.name)
self._attr_name = friendly_name_format.format(gw_dev.name)
self._unsub_updates = None
self._attr_unique_id = f"{gw_dev.gw_id}-{source}-{var}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, gw_dev.gw_id)},
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
name=gw_dev.name,
sw_version=gw_dev.gw_version,
)
async def async_added_to_hass(self) -> None:
"""Subscribe to updates from the component."""
_LOGGER.debug("Added OpenTherm Gateway sensor %s", self._friendly_name)
_LOGGER.debug("Added OpenTherm Gateway sensor %s", self._attr_name)
self._unsub_updates = async_dispatcher_connect(
self.hass, self._gateway.update_signal, self.receive_report
)
async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from updates from the component."""
_LOGGER.debug("Removing OpenTherm Gateway sensor %s", self._friendly_name)
_LOGGER.debug("Removing OpenTherm Gateway sensor %s", self._attr_name)
self._unsub_updates()
@property
def available(self):
"""Return availability of the sensor."""
return self._value is not None
@property
def entity_registry_enabled_default(self):
"""Disable sensors by default."""
return False
return self._attr_native_value is not None
@callback
def receive_report(self, status):
@ -96,41 +99,5 @@ class OpenThermSensor(SensorEntity):
value = status[self._source].get(self._var)
if isinstance(value, float):
value = f"{value:2.1f}"
self._value = value
self._attr_native_value = value
self.async_write_ha_state()
@property
def name(self):
"""Return the friendly name of the sensor."""
return self._friendly_name
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._gateway.gw_id)},
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
name=self._gateway.name,
sw_version=self._gateway.gw_version,
)
@property
def unique_id(self):
"""Return a unique ID."""
return f"{self._gateway.gw_id}-{self._source}-{self._var}"
@property
def device_class(self):
"""Return the device class."""
return self._device_class
@property
def native_value(self):
"""Return the state of the device."""
return self._value
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit