Skip to content

Commit

Permalink
Correct device serial for ViCare integration (#125125)
Browse files Browse the repository at this point in the history
* expose correct serial

* adapt inits

* adjust _build_entities

* adapt inits

* add serial data point

* update snapshot

* apply suggestions

* apply suggestions
  • Loading branch information
CFenner authored Sep 3, 2024
1 parent fc24843 commit f34b449
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 133 deletions.
78 changes: 27 additions & 51 deletions homeassistant/components/vicare/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,61 +112,36 @@ def _build_entities(

entities: list[ViCareBinarySensor] = []
for device in device_list:
entities.extend(_build_entities_for_device(device.api, device.config))
# add device entities
entities.extend(
_build_entities_for_component(
get_circuits(device.api), device.config, CIRCUIT_SENSORS
ViCareBinarySensor(
description,
device.config,
device.api,
)
for description in GLOBAL_SENSORS
if is_supported(description.key, description, device.api)
)
entities.extend(
_build_entities_for_component(
get_burners(device.api), device.config, BURNER_SENSORS
# add component entities
for component_list, entity_description_list in (
(get_circuits(device.api), CIRCUIT_SENSORS),
(get_burners(device.api), BURNER_SENSORS),
(get_compressors(device.api), COMPRESSOR_SENSORS),
):
entities.extend(
ViCareBinarySensor(
description,
device.config,
device.api,
component,
)
for component in component_list
for description in entity_description_list
if is_supported(description.key, description, component)
)
)
entities.extend(
_build_entities_for_component(
get_compressors(device.api), device.config, COMPRESSOR_SENSORS
)
)
return entities


def _build_entities_for_device(
device: PyViCareDevice,
device_config: PyViCareDeviceConfig,
) -> list[ViCareBinarySensor]:
"""Create device specific ViCare binary sensor entities."""

return [
ViCareBinarySensor(
device_config,
device,
description,
)
for description in GLOBAL_SENSORS
if is_supported(description.key, description, device)
]


def _build_entities_for_component(
components: list[PyViCareHeatingDeviceComponent],
device_config: PyViCareDeviceConfig,
entity_descriptions: tuple[ViCareBinarySensorEntityDescription, ...],
) -> list[ViCareBinarySensor]:
"""Create component specific ViCare binary sensor entities."""

return [
ViCareBinarySensor(
device_config,
component,
description,
)
for component in components
for description in entity_descriptions
if is_supported(description.key, description, component)
]


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
Expand All @@ -190,12 +165,13 @@ class ViCareBinarySensor(ViCareEntity, BinarySensorEntity):

def __init__(
self,
device_config: PyViCareDeviceConfig,
api: PyViCareDevice | PyViCareHeatingDeviceComponent,
description: ViCareBinarySensorEntityDescription,
device_config: PyViCareDeviceConfig,
device: PyViCareDevice,
component: PyViCareHeatingDeviceComponent | None = None,
) -> None:
"""Initialize the sensor."""
super().__init__(device_config, api, description.key)
super().__init__(description.key, device_config, device, component)
self.entity_description = description

@property
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/vicare/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def _build_entities(

return [
ViCareButton(
description,
device.config,
device.api,
description,
)
for device in device_list
for description in BUTTON_DESCRIPTIONS
Expand Down Expand Up @@ -87,12 +87,12 @@ class ViCareButton(ViCareEntity, ButtonEntity):

def __init__(
self,
description: ViCareButtonEntityDescription,
device_config: PyViCareDeviceConfig,
device: PyViCareDevice,
description: ViCareButtonEntityDescription,
) -> None:
"""Initialize the button."""
super().__init__(device_config, device, description.key)
super().__init__(description.key, device_config, device)
self.entity_description = description

def press(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/vicare/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def __init__(
circuit: PyViCareHeatingCircuit,
) -> None:
"""Initialize the climate device."""
super().__init__(device_config, device, circuit.id)
super().__init__(circuit.id, device_config, device)
self._circuit = circuit
self._attributes: dict[str, Any] = {}
self._attributes["vicare_programs"] = self._circuit.getPrograms()
Expand Down
16 changes: 11 additions & 5 deletions homeassistant/components/vicare/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from PyViCare.PyViCareDevice import Device as PyViCareDevice
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareHeatingDevice import (
HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent,
)

from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
Expand All @@ -16,21 +19,24 @@ class ViCareEntity(Entity):

def __init__(
self,
unique_id_suffix: str,
device_config: PyViCareDeviceConfig,
device: PyViCareDevice,
unique_id_suffix: str,
component: PyViCareHeatingDeviceComponent | None = None,
) -> None:
"""Initialize the entity."""
self._api = device
self._api: PyViCareDevice | PyViCareHeatingDeviceComponent = (
component if component else device
)

self._attr_unique_id = f"{device_config.getConfig().serial}-{unique_id_suffix}"
# valid for compressors, circuits, burners (HeatingDeviceWithComponent)
if hasattr(device, "id"):
self._attr_unique_id += f"-{device.id}"
if component:
self._attr_unique_id += f"-{component.id}"

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_config.getConfig().serial)},
serial_number=device_config.getConfig().serial,
serial_number=device.getSerial(),
name=device_config.getModel(),
manufacturer="Viessmann",
model=device_config.getModel(),
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/vicare/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __init__(
device: PyViCareDevice,
) -> None:
"""Initialize the fan entity."""
super().__init__(device_config, device, self._attr_translation_key)
super().__init__(self._attr_translation_key, device_config, device)

def update(self) -> None:
"""Update state of fan."""
Expand Down
39 changes: 20 additions & 19 deletions homeassistant/components/vicare/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,30 +245,30 @@ def _build_entities(
) -> list[ViCareNumber]:
"""Create ViCare number entities for a device."""

entities: list[ViCareNumber] = [
ViCareNumber(
device.config,
device.api,
description,
entities: list[ViCareNumber] = []
for device in device_list:
# add device entities
entities.extend(
ViCareNumber(
description,
device.config,
device.api,
)
for description in DEVICE_ENTITY_DESCRIPTIONS
if is_supported(description.key, description, device.api)
)
for device in device_list
for description in DEVICE_ENTITY_DESCRIPTIONS
if is_supported(description.key, description, device.api)
]

entities.extend(
[
# add component entities
entities.extend(
ViCareNumber(
description,
device.config,
device.api,
circuit,
description,
)
for device in device_list
for circuit in get_circuits(device.api)
for description in CIRCUIT_ENTITY_DESCRIPTIONS
if is_supported(description.key, description, circuit)
]
)
)
return entities


Expand All @@ -295,12 +295,13 @@ class ViCareNumber(ViCareEntity, NumberEntity):

def __init__(
self,
device_config: PyViCareDeviceConfig,
api: PyViCareDevice | PyViCareHeatingDeviceComponent,
description: ViCareNumberEntityDescription,
device_config: PyViCareDeviceConfig,
device: PyViCareDevice,
component: PyViCareHeatingDeviceComponent | None = None,
) -> None:
"""Initialize the number."""
super().__init__(device_config, api, description.key)
super().__init__(description.key, device_config, device, component)
self.entity_description = description

@property
Expand Down
79 changes: 27 additions & 52 deletions homeassistant/components/vicare/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,6 @@ class ViCareSensorEntityDescription(SensorEntityDescription, ViCareRequiredKeysM
),
)


CIRCUIT_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
ViCareSensorEntityDescription(
key="supply_temperature",
Expand Down Expand Up @@ -865,61 +864,36 @@ def _build_entities(

entities: list[ViCareSensor] = []
for device in device_list:
entities.extend(_build_entities_for_device(device.api, device.config))
# add device entities
entities.extend(
_build_entities_for_component(
get_circuits(device.api), device.config, CIRCUIT_SENSORS
ViCareSensor(
description,
device.config,
device.api,
)
for description in GLOBAL_SENSORS
if is_supported(description.key, description, device.api)
)
entities.extend(
_build_entities_for_component(
get_burners(device.api), device.config, BURNER_SENSORS
)
)
entities.extend(
_build_entities_for_component(
get_compressors(device.api), device.config, COMPRESSOR_SENSORS
# add component entities
for component_list, entity_description_list in (
(get_circuits(device.api), CIRCUIT_SENSORS),
(get_burners(device.api), BURNER_SENSORS),
(get_compressors(device.api), COMPRESSOR_SENSORS),
):
entities.extend(
ViCareSensor(
description,
device.config,
device.api,
component,
)
for component in component_list
for description in entity_description_list
if is_supported(description.key, description, component)
)
)
return entities


def _build_entities_for_device(
device: PyViCareDevice,
device_config: PyViCareDeviceConfig,
) -> list[ViCareSensor]:
"""Create device specific ViCare sensor entities."""

return [
ViCareSensor(
device_config,
device,
description,
)
for description in GLOBAL_SENSORS
if is_supported(description.key, description, device)
]


def _build_entities_for_component(
components: list[PyViCareHeatingDeviceComponent],
device_config: PyViCareDeviceConfig,
entity_descriptions: tuple[ViCareSensorEntityDescription, ...],
) -> list[ViCareSensor]:
"""Create component specific ViCare sensor entities."""

return [
ViCareSensor(
device_config,
component,
description,
)
for component in components
for description in entity_descriptions
if is_supported(description.key, description, component)
]


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
Expand All @@ -945,12 +919,13 @@ class ViCareSensor(ViCareEntity, SensorEntity):

def __init__(
self,
device_config: PyViCareDeviceConfig,
api: PyViCareDevice | PyViCareHeatingDeviceComponent,
description: ViCareSensorEntityDescription,
device_config: PyViCareDeviceConfig,
device: PyViCareDevice,
component: PyViCareHeatingDeviceComponent | None = None,
) -> None:
"""Initialize the sensor."""
super().__init__(device_config, api, description.key)
super().__init__(description.key, device_config, device, component)
self.entity_description = description

@property
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/vicare/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(
circuit: PyViCareHeatingCircuit,
) -> None:
"""Initialize the DHW water_heater device."""
super().__init__(device_config, device, circuit.id)
super().__init__(circuit.id, device_config, device)
self._circuit = circuit
self._attributes: dict[str, Any] = {}

Expand Down
17 changes: 17 additions & 0 deletions tests/components/vicare/fixtures/Vitodens300W.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
{
"data": [
{
"apiVersion": 1,
"commands": {},
"deviceId": "0",
"feature": "device.serial",
"gatewayId": "################",
"isEnabled": true,
"isReady": true,
"properties": {
"value": {
"type": "string",
"value": "################"
}
},
"timestamp": "2024-03-20T01:29:35.549Z",
"uri": "https://api.viessmann.com/iot/v1/features/installations/#######/gateways/################/devices/0/features/device.serial"
},
{
"properties": {},
"commands": {},
Expand Down
Loading

0 comments on commit f34b449

Please sign in to comment.