Use shorthand attributes in Vicare (#99915)

pull/100007/head
Joost Lekkerkerker 2023-09-09 23:18:41 +02:00 committed by GitHub
parent 23f4ccd4f1
commit eeaca8ae3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 188 deletions

View File

@ -196,23 +196,18 @@ class ViCareBinarySensor(BinarySensorEntity):
self._api = api
self.entity_description = description
self._device_config = device_config
self._state = None
@property
def device_info(self) -> DeviceInfo:
"""Return device info for this device."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_config.getConfig().serial)},
name=self._device_config.getModel(),
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_config.getConfig().serial)},
name=device_config.getModel(),
manufacturer="Viessmann",
model=self._device_config.getModel(),
model=device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
@property
def available(self):
"""Return True if entity is available."""
return self._state is not None
return self._attr_is_on is not None
@property
def unique_id(self) -> str:
@ -224,16 +219,11 @@ class ViCareBinarySensor(BinarySensorEntity):
return f"{tmp_id}-{self._api.id}"
return tmp_id
@property
def is_on(self):
"""Return the state of the sensor."""
return self._state
def update(self):
"""Update state of sensor."""
try:
with suppress(PyViCareNotSupportedFeatureError):
self._state = self.entity_description.value_getter(self._api)
self._attr_is_on = self.entity_description.value_getter(self._api)
except requests.exceptions.ConnectionError:
_LOGGER.error("Unable to retrieve data from ViCare server")
except ValueError:

View File

@ -104,6 +104,13 @@ class ViCareButton(ButtonEntity):
self.entity_description = description
self._device_config = device_config
self._api = api
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_config.getConfig().serial)},
name=device_config.getModel(),
manufacturer="Viessmann",
model=device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
def press(self) -> None:
"""Handle the button press."""
@ -119,17 +126,6 @@ class ViCareButton(ButtonEntity):
except PyViCareInvalidDataError as invalid_data_exception:
_LOGGER.error("Invalid data from Vicare server: %s", invalid_data_exception)
@property
def device_info(self) -> DeviceInfo:
"""Return device info for this device."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_config.getConfig().serial)},
name=self._device_config.getModel(),
manufacturer="Viessmann",
model=self._device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
@property
def unique_id(self) -> str:
"""Return unique ID for this device."""

View File

@ -36,13 +36,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
CONF_HEATING_TYPE,
DOMAIN,
VICARE_API,
VICARE_DEVICE_CONFIG,
VICARE_NAME,
)
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG, VICARE_NAME
_LOGGER = logging.getLogger(__name__)
@ -126,7 +120,6 @@ async def async_setup_entry(
api,
circuit,
hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG],
config_entry.data[CONF_HEATING_TYPE],
)
entities.append(entity)
@ -149,35 +142,26 @@ class ViCareClimate(ClimateEntity):
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
)
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_min_temp = VICARE_TEMP_HEATING_MIN
_attr_max_temp = VICARE_TEMP_HEATING_MAX
_attr_target_temperature_step = PRECISION_WHOLE
_attr_preset_modes = list(HA_TO_VICARE_PRESET_HEATING)
def __init__(self, name, api, circuit, device_config, heating_type):
def __init__(self, name, api, circuit, device_config):
"""Initialize the climate device."""
self._name = name
self._state = None
self._attr_name = name
self._api = api
self._circuit = circuit
self._device_config = device_config
self._attributes = {}
self._target_temperature = None
self._current_mode = None
self._current_temperature = None
self._current_program = None
self._heating_type = heating_type
self._current_action = None
@property
def unique_id(self) -> str:
"""Return unique ID for this device."""
return f"{self._device_config.getConfig().serial}-{self._circuit.id}"
@property
def device_info(self) -> DeviceInfo:
"""Return device info for this device."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_config.getConfig().serial)},
name=self._device_config.getModel(),
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_config.getConfig().serial)},
name=device_config.getModel(),
manufacturer="Viessmann",
model=self._device_config.getModel(),
model=device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
@ -193,27 +177,29 @@ class ViCareClimate(ClimateEntity):
_supply_temperature = self._circuit.getSupplyTemperature()
if _room_temperature is not None:
self._current_temperature = _room_temperature
self._attr_current_temperature = _room_temperature
elif _supply_temperature is not None:
self._current_temperature = _supply_temperature
self._attr_current_temperature = _supply_temperature
else:
self._current_temperature = None
self._attr_current_temperature = None
with suppress(PyViCareNotSupportedFeatureError):
self._current_program = self._circuit.getActiveProgram()
with suppress(PyViCareNotSupportedFeatureError):
self._target_temperature = self._circuit.getCurrentDesiredTemperature()
self._attr_target_temperature = (
self._circuit.getCurrentDesiredTemperature()
)
with suppress(PyViCareNotSupportedFeatureError):
self._current_mode = self._circuit.getActiveMode()
# Update the generic device attributes
self._attributes = {}
self._attributes["room_temperature"] = _room_temperature
self._attributes["active_vicare_program"] = self._current_program
self._attributes["active_vicare_mode"] = self._current_mode
self._attributes = {
"room_temperature": _room_temperature,
"active_vicare_program": self._current_program,
"active_vicare_mode": self._current_mode,
}
with suppress(PyViCareNotSupportedFeatureError):
self._attributes[
@ -248,21 +234,6 @@ class ViCareClimate(ClimateEntity):
except PyViCareInvalidDataError as invalid_data_exception:
_LOGGER.error("Invalid data from Vicare server: %s", invalid_data_exception)
@property
def name(self):
"""Return the name of the climate device."""
return self._name
@property
def current_temperature(self):
"""Return the current temperature."""
return self._current_temperature
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
@property
def hvac_mode(self) -> HVACMode | None:
"""Return current hvac mode."""
@ -313,37 +284,17 @@ class ViCareClimate(ClimateEntity):
return HVACAction.HEATING
return HVACAction.IDLE
@property
def min_temp(self):
"""Return the minimum temperature."""
return VICARE_TEMP_HEATING_MIN
@property
def max_temp(self):
"""Return the maximum temperature."""
return VICARE_TEMP_HEATING_MAX
@property
def target_temperature_step(self) -> float:
"""Set target temperature step to wholes."""
return PRECISION_WHOLE
def set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperatures."""
if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None:
self._circuit.setProgramTemperature(self._current_program, temp)
self._target_temperature = temp
self._attr_target_temperature = temp
@property
def preset_mode(self):
"""Return the current preset mode, e.g., home, away, temp."""
return VICARE_TO_HA_PRESET_HEATING.get(self._current_program)
@property
def preset_modes(self):
"""Return the available preset mode."""
return list(HA_TO_VICARE_PRESET_HEATING)
def set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode and deactivate any existing programs."""
vicare_program = HA_TO_VICARE_PRESET_HEATING.get(preset_mode)

View File

@ -673,7 +673,6 @@ class ViCareSensor(SensorEntity):
self._attr_name = name
self._api = api
self._device_config = device_config
self._state = None
@property
def device_info(self) -> DeviceInfo:
@ -689,7 +688,7 @@ class ViCareSensor(SensorEntity):
@property
def available(self):
"""Return True if entity is available."""
return self._state is not None
return self._attr_native_value is not None
@property
def unique_id(self) -> str:
@ -701,16 +700,13 @@ class ViCareSensor(SensorEntity):
return f"{tmp_id}-{self._api.id}"
return tmp_id
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
def update(self):
"""Update state of sensor."""
try:
with suppress(PyViCareNotSupportedFeatureError):
self._state = self.entity_description.value_getter(self._api)
self._attr_native_value = self.entity_description.value_getter(
self._api
)
if self.entity_description.unit_getter:
vicare_unit = self.entity_description.unit_getter(self._api)

View File

@ -15,23 +15,12 @@ from homeassistant.components.water_heater import (
WaterHeaterEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_TEMPERATURE,
PRECISION_TENTHS,
PRECISION_WHOLE,
UnitOfTemperature,
)
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_TENTHS, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
CONF_HEATING_TYPE,
DOMAIN,
VICARE_API,
VICARE_DEVICE_CONFIG,
VICARE_NAME,
)
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG, VICARE_NAME
_LOGGER = logging.getLogger(__name__)
@ -95,7 +84,6 @@ async def async_setup_entry(
api,
circuit,
hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG],
config_entry.data[CONF_HEATING_TYPE],
)
entities.append(entity)
@ -107,30 +95,37 @@ class ViCareWater(WaterHeaterEntity):
_attr_precision = PRECISION_TENTHS
_attr_supported_features = WaterHeaterEntityFeature.TARGET_TEMPERATURE
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_min_temp = VICARE_TEMP_WATER_MIN
_attr_max_temp = VICARE_TEMP_WATER_MAX
_attr_operation_list = list(HA_TO_VICARE_HVAC_DHW)
def __init__(self, name, api, circuit, device_config, heating_type):
def __init__(self, name, api, circuit, device_config):
"""Initialize the DHW water_heater device."""
self._name = name
self._state = None
self._attr_name = name
self._api = api
self._circuit = circuit
self._device_config = device_config
self._attributes = {}
self._target_temperature = None
self._current_temperature = None
self._current_mode = None
self._heating_type = heating_type
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_config.getConfig().serial)},
name=device_config.getModel(),
manufacturer="Viessmann",
model=device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
def update(self) -> None:
"""Let HA know there has been an update from the ViCare API."""
try:
with suppress(PyViCareNotSupportedFeatureError):
self._current_temperature = (
self._attr_current_temperature = (
self._api.getDomesticHotWaterStorageTemperature()
)
with suppress(PyViCareNotSupportedFeatureError):
self._target_temperature = (
self._attr_target_temperature = (
self._api.getDomesticHotWaterDesiredTemperature()
)
@ -146,69 +141,13 @@ class ViCareWater(WaterHeaterEntity):
except PyViCareInvalidDataError as invalid_data_exception:
_LOGGER.error("Invalid data from Vicare server: %s", invalid_data_exception)
@property
def unique_id(self) -> str:
"""Return unique ID for this device."""
return f"{self._device_config.getConfig().serial}-{self._circuit.id}"
@property
def device_info(self) -> DeviceInfo:
"""Return device info for this device."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_config.getConfig().serial)},
name=self._device_config.getModel(),
manufacturer="Viessmann",
model=self._device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
@property
def name(self):
"""Return the name of the water_heater device."""
return self._name
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return UnitOfTemperature.CELSIUS
@property
def current_temperature(self):
"""Return the current temperature."""
return self._current_temperature
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
def set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperatures."""
if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None:
self._api.setDomesticHotWaterTemperature(temp)
self._target_temperature = temp
@property
def min_temp(self):
"""Return the minimum temperature."""
return VICARE_TEMP_WATER_MIN
@property
def max_temp(self):
"""Return the maximum temperature."""
return VICARE_TEMP_WATER_MAX
@property
def target_temperature_step(self) -> float:
"""Set target temperature step to wholes."""
return PRECISION_WHOLE
self._attr_target_temperature = temp
@property
def current_operation(self):
"""Return current operation ie. heat, cool, idle."""
return VICARE_TO_HA_HVAC_DHW.get(self._current_mode)
@property
def operation_list(self):
"""Return the list of available operation modes."""
return list(HA_TO_VICARE_HVAC_DHW)