mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
vicare: Fix dynamic unit of measurement (#64782)
This commit is contained in:
parent
f2a3f758af
commit
157f145ea9
@ -117,73 +117,57 @@ GLOBAL_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
|
|||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_DHW_GAS_CONSUMPTION_TODAY,
|
key=SENSOR_DHW_GAS_CONSUMPTION_TODAY,
|
||||||
name="Hot water gas consumption today",
|
name="Hot water gas consumption today",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterToday(),
|
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterToday(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_DHW_GAS_CONSUMPTION_THIS_WEEK,
|
key=SENSOR_DHW_GAS_CONSUMPTION_THIS_WEEK,
|
||||||
name="Hot water gas consumption this week",
|
name="Hot water gas consumption this week",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisWeek(),
|
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisWeek(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_DHW_GAS_CONSUMPTION_THIS_MONTH,
|
key=SENSOR_DHW_GAS_CONSUMPTION_THIS_MONTH,
|
||||||
name="Hot water gas consumption this month",
|
name="Hot water gas consumption this month",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisMonth(),
|
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisMonth(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_DHW_GAS_CONSUMPTION_THIS_YEAR,
|
key=SENSOR_DHW_GAS_CONSUMPTION_THIS_YEAR,
|
||||||
name="Hot water gas consumption this year",
|
name="Hot water gas consumption this year",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisYear(),
|
value_getter=lambda api: api.getGasConsumptionDomesticHotWaterThisYear(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
unit_getter=lambda api: api.getGasConsumptionDomesticHotWaterUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_GAS_CONSUMPTION_TODAY,
|
key=SENSOR_GAS_CONSUMPTION_TODAY,
|
||||||
name="Heating gas consumption today",
|
name="Heating gas consumption today",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionHeatingToday(),
|
value_getter=lambda api: api.getGasConsumptionHeatingToday(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_GAS_CONSUMPTION_THIS_WEEK,
|
key=SENSOR_GAS_CONSUMPTION_THIS_WEEK,
|
||||||
name="Heating gas consumption this week",
|
name="Heating gas consumption this week",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionHeatingThisWeek(),
|
value_getter=lambda api: api.getGasConsumptionHeatingThisWeek(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_GAS_CONSUMPTION_THIS_MONTH,
|
key=SENSOR_GAS_CONSUMPTION_THIS_MONTH,
|
||||||
name="Heating gas consumption this month",
|
name="Heating gas consumption this month",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionHeatingThisMonth(),
|
value_getter=lambda api: api.getGasConsumptionHeatingThisMonth(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
key=SENSOR_GAS_CONSUMPTION_THIS_YEAR,
|
key=SENSOR_GAS_CONSUMPTION_THIS_YEAR,
|
||||||
name="Heating gas consumption this year",
|
name="Heating gas consumption this year",
|
||||||
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
||||||
value_getter=lambda api: api.getGasConsumptionHeatingThisYear(),
|
value_getter=lambda api: api.getGasConsumptionHeatingThisYear(),
|
||||||
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
unit_getter=lambda api: api.getGasConsumptionHeatingUnit(),
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
ViCareSensorEntityDescription(
|
ViCareSensorEntityDescription(
|
||||||
@ -339,15 +323,6 @@ def _build_entity(name, vicare_api, device_config, sensor):
|
|||||||
_LOGGER.debug("Found device %s", name)
|
_LOGGER.debug("Found device %s", name)
|
||||||
try:
|
try:
|
||||||
sensor.value_getter(vicare_api)
|
sensor.value_getter(vicare_api)
|
||||||
|
|
||||||
if sensor.unit_getter:
|
|
||||||
with suppress(PyViCareNotSupportedFeatureError):
|
|
||||||
vicare_unit = sensor.unit_getter(vicare_api)
|
|
||||||
if vicare_unit is not None:
|
|
||||||
sensor.device_class = VICARE_UNIT_TO_DEVICE_CLASS.get(vicare_unit)
|
|
||||||
sensor.native_unit_of_measurement = (
|
|
||||||
VICARE_UNIT_TO_UNIT_OF_MEASUREMENT.get(vicare_unit)
|
|
||||||
)
|
|
||||||
_LOGGER.debug("Found entity %s", name)
|
_LOGGER.debug("Found entity %s", name)
|
||||||
except PyViCareNotSupportedFeatureError:
|
except PyViCareNotSupportedFeatureError:
|
||||||
_LOGGER.info("Feature not supported %s", name)
|
_LOGGER.info("Feature not supported %s", name)
|
||||||
@ -487,6 +462,16 @@ class ViCareSensor(SensorEntity):
|
|||||||
try:
|
try:
|
||||||
with suppress(PyViCareNotSupportedFeatureError):
|
with suppress(PyViCareNotSupportedFeatureError):
|
||||||
self._state = self.entity_description.value_getter(self._api)
|
self._state = self.entity_description.value_getter(self._api)
|
||||||
|
|
||||||
|
if self.entity_description.unit_getter:
|
||||||
|
vicare_unit = self.entity_description.unit_getter(self._api)
|
||||||
|
if vicare_unit is not None:
|
||||||
|
self._attr_device_class = VICARE_UNIT_TO_DEVICE_CLASS.get(
|
||||||
|
vicare_unit
|
||||||
|
)
|
||||||
|
self._attr_native_unit_of_measurement = (
|
||||||
|
VICARE_UNIT_TO_UNIT_OF_MEASUREMENT.get(vicare_unit)
|
||||||
|
)
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.error("Unable to retrieve data from ViCare server")
|
_LOGGER.error("Unable to retrieve data from ViCare server")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user