From 5721225dded1f5bc6690d9075a0d3a9a8a782e94 Mon Sep 17 00:00:00 2001 From: SukramJ Date: Sat, 3 Oct 2020 16:35:04 +0200 Subject: [PATCH] Reorg channel handling for Homematic IP Cloud (#41118) --- .../components/homematicip_cloud/__init__.py | 2 +- .../homematicip_cloud/binary_sensor.py | 12 ++--- .../homematicip_cloud/generic_entity.py | 48 ++++++++++++++----- .../components/homematicip_cloud/light.py | 31 +++--------- .../components/homematicip_cloud/sensor.py | 16 +++---- .../components/homematicip_cloud/switch.py | 30 ++++-------- 6 files changed, 66 insertions(+), 73 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/__init__.py b/homeassistant/components/homematicip_cloud/__init__.py index 47da33e86da..e53307c533b 100644 --- a/homeassistant/components/homematicip_cloud/__init__.py +++ b/homeassistant/components/homematicip_cloud/__init__.py @@ -99,7 +99,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool # Add the HAP name from configuration if set. hapname = home.label if not home.name else f"{home.name} {home.label}" device_registry.async_get_or_create( - config_entry_id=home.id, + config_entry_id=entry.entry_id, identifiers={(DOMAIN, home.id)}, manufacturer="eQ-3", name=hapname, diff --git a/homeassistant/components/homematicip_cloud/binary_sensor.py b/homeassistant/components/homematicip_cloud/binary_sensor.py index 440dc31788f..3c04fd7410a 100644 --- a/homeassistant/components/homematicip_cloud/binary_sensor.py +++ b/homeassistant/components/homematicip_cloud/binary_sensor.py @@ -128,9 +128,9 @@ async def async_setup_entry( for group in hap.home.groups: if isinstance(group, AsyncSecurityGroup): - entities.append(HomematicipSecuritySensorGroup(hap, group)) + entities.append(HomematicipSecuritySensorGroup(hap, device=group)) elif isinstance(group, AsyncSecurityZoneGroup): - entities.append(HomematicipSecurityZoneSensorGroup(hap, group)) + entities.append(HomematicipSecurityZoneSensorGroup(hap, device=group)) if entities: async_add_entities(entities) @@ -361,7 +361,7 @@ class HomematicipSunshineSensor(HomematicipGenericEntity, BinarySensorEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize sunshine sensor.""" - super().__init__(hap, device, "Sunshine") + super().__init__(hap, device, post="Sunshine") @property def device_class(self) -> str: @@ -390,7 +390,7 @@ class HomematicipBatterySensor(HomematicipGenericEntity, BinarySensorEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize battery sensor.""" - super().__init__(hap, device, "Battery") + super().__init__(hap, device, post="Battery") @property def device_class(self) -> str: @@ -429,7 +429,7 @@ class HomematicipSecurityZoneSensorGroup(HomematicipGenericEntity, BinarySensorE def __init__(self, hap: HomematicipHAP, device, post: str = "SecurityZone") -> None: """Initialize security zone group.""" device.modelType = f"HmIP-{post}" - super().__init__(hap, device, post) + super().__init__(hap, device, post=post) @property def device_class(self) -> str: @@ -485,7 +485,7 @@ class HomematicipSecuritySensorGroup( def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize security group.""" - super().__init__(hap, device, "Sensors") + super().__init__(hap, device, post="Sensors") @property def device_state_attributes(self) -> Dict[str, Any]: diff --git a/homeassistant/components/homematicip_cloud/generic_entity.py b/homeassistant/components/homematicip_cloud/generic_entity.py index 3a19c1b2afe..ce8b44f5702 100644 --- a/homeassistant/components/homematicip_cloud/generic_entity.py +++ b/homeassistant/components/homematicip_cloud/generic_entity.py @@ -70,12 +70,19 @@ GROUP_ATTRIBUTES = { class HomematicipGenericEntity(Entity): """Representation of the HomematicIP generic entity.""" - def __init__(self, hap: HomematicipHAP, device, post: Optional[str] = None) -> None: + def __init__( + self, + hap: HomematicipHAP, + device, + post: Optional[str] = None, + channel: Optional[int] = None, + ) -> None: """Initialize the generic entity.""" self._hap = hap self._home = hap.home self._device = device - self.post = post + self._post = post + self._channel = channel # Marker showing that the HmIP device hase been removed. self.hmip_device_removed = False _LOGGER.info("Setting up %s (%s)", self.name, self._device.modelType) @@ -94,6 +101,7 @@ class HomematicipGenericEntity(Entity): "manufacturer": self._device.oem, "model": self._device.modelType, "sw_version": self._device.firmwareVersion, + # Link to the homematic ip access point. "via_device": (HMIPC_DOMAIN, self._device.homeId), } return None @@ -167,18 +175,28 @@ class HomematicipGenericEntity(Entity): @property def name(self) -> str: """Return the name of the generic entity.""" - name = self._device.label - if name and self._home.name: - name = f"{self._home.name} {name}" - if name and self.post: - name = f"{name} {self.post}" - return name - def _get_label_by_channel(self, channel: int) -> str: - """Return the name of the channel.""" - name = self._device.functionalChannels[channel].label + name = None + # Try to get a label from a channel. + if hasattr(self._device, "functionalChannels"): + if self._channel: + name = self._device.functionalChannels[self._channel].label + else: + if len(self._device.functionalChannels) > 1: + name = self._device.functionalChannels[1].label + + # Use device label, if name is not defined by channel label. + if not name: + name = self._device.label + if self._post: + name = f"{name} {self._post}" + elif self._channel: + name = f"{name} Channel{self._channel}" + + # Add a prefix to the name if the homematic ip home has a name. if name and self._home.name: name = f"{self._home.name} {name}" + return name @property @@ -194,7 +212,13 @@ class HomematicipGenericEntity(Entity): @property def unique_id(self) -> str: """Return a unique ID.""" - return f"{self.__class__.__name__}_{self._device.id}" + unique_id = f"{self.__class__.__name__}_{self._device.id}" + if self._channel: + unique_id = ( + f"{self.__class__.__name__}_Channel{self._channel}_{self._device.id}" + ) + + return unique_id @property def icon(self) -> Optional[str]: diff --git a/homeassistant/components/homematicip_cloud/light.py b/homeassistant/components/homematicip_cloud/light.py index 72737122372..e4d85c00b19 100644 --- a/homeassistant/components/homematicip_cloud/light.py +++ b/homeassistant/components/homematicip_cloud/light.py @@ -71,14 +71,6 @@ class HomematicipLight(HomematicipGenericEntity, LightEntity): """Initialize the light entity.""" super().__init__(hap, device) - @property - def name(self) -> str: - """Return the name of the multi switch channel.""" - label = self._get_label_by_channel(1) - if label: - return label - return super().name - @property def is_on(self) -> bool: """Return true if light is on.""" @@ -149,11 +141,10 @@ class HomematicipNotificationLight(HomematicipGenericEntity, LightEntity): def __init__(self, hap: HomematicipHAP, device, channel: int) -> None: """Initialize the notification light entity.""" - self.channel = channel - if self.channel == 2: - super().__init__(hap, device, "Top") + if channel == 2: + super().__init__(hap, device, post="Top", channel=channel) else: - super().__init__(hap, device, "Bottom") + super().__init__(hap, device, post="Bottom", channel=channel) self._color_switcher = { RGBColorState.WHITE: [0.0, 0.0], @@ -167,7 +158,7 @@ class HomematicipNotificationLight(HomematicipGenericEntity, LightEntity): @property def _func_channel(self) -> NotificationLightChannel: - return self._device.functionalChannels[self.channel] + return self._device.functionalChannels[self._channel] @property def is_on(self) -> bool: @@ -198,14 +189,6 @@ class HomematicipNotificationLight(HomematicipGenericEntity, LightEntity): return state_attr - @property - def name(self) -> str: - """Return the name of the notification light sensor.""" - label = self._get_label_by_channel(self.channel) - if label: - return label - return f"{super().name} Notification" - @property def supported_features(self) -> int: """Flag supported features.""" @@ -214,7 +197,7 @@ class HomematicipNotificationLight(HomematicipGenericEntity, LightEntity): @property def unique_id(self) -> str: """Return a unique ID.""" - return f"{self.__class__.__name__}_{self.post}_{self._device.id}" + return f"{self.__class__.__name__}_{self._post}_{self._device.id}" async def async_turn_on(self, **kwargs) -> None: """Turn the light on.""" @@ -237,7 +220,7 @@ class HomematicipNotificationLight(HomematicipGenericEntity, LightEntity): transition = kwargs.get(ATTR_TRANSITION, 0.5) await self._device.set_rgb_dim_level_with_time( - channelIndex=self.channel, + channelIndex=self._channel, rgb=simple_rgb_color, dimLevel=dim_level, onTime=0, @@ -250,7 +233,7 @@ class HomematicipNotificationLight(HomematicipGenericEntity, LightEntity): transition = kwargs.get(ATTR_TRANSITION, 0.5) await self._device.set_rgb_dim_level_with_time( - channelIndex=self.channel, + channelIndex=self._channel, rgb=simple_rgb_color, dimLevel=0.0, onTime=0, diff --git a/homeassistant/components/homematicip_cloud/sensor.py b/homeassistant/components/homematicip_cloud/sensor.py index 082d7e9e355..77d7560e622 100644 --- a/homeassistant/components/homematicip_cloud/sensor.py +++ b/homeassistant/components/homematicip_cloud/sensor.py @@ -127,7 +127,7 @@ class HomematicipAccesspointStatus(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP) -> None: """Initialize access point status entity.""" - super().__init__(hap, hap.home, "Duty Cycle") + super().__init__(hap, device=hap.home, post="Duty Cycle") @property def device_info(self) -> Dict[str, Any]: @@ -176,7 +176,7 @@ class HomematicipHeatingThermostat(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize heating thermostat device.""" - super().__init__(hap, device, "Heating") + super().__init__(hap, device, post="Heating") @property def icon(self) -> str: @@ -205,7 +205,7 @@ class HomematicipHumiditySensor(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the thermometer device.""" - super().__init__(hap, device, "Humidity") + super().__init__(hap, device, post="Humidity") @property def device_class(self) -> str: @@ -228,7 +228,7 @@ class HomematicipTemperatureSensor(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the thermometer device.""" - super().__init__(hap, device, "Temperature") + super().__init__(hap, device, post="Temperature") @property def device_class(self) -> str: @@ -265,7 +265,7 @@ class HomematicipIlluminanceSensor(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the device.""" - super().__init__(hap, device, "Illuminance") + super().__init__(hap, device, post="Illuminance") @property def device_class(self) -> str: @@ -303,7 +303,7 @@ class HomematicipPowerSensor(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the device.""" - super().__init__(hap, device, "Power") + super().__init__(hap, device, post="Power") @property def device_class(self) -> str: @@ -326,7 +326,7 @@ class HomematicipWindspeedSensor(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the windspeed sensor.""" - super().__init__(hap, device, "Windspeed") + super().__init__(hap, device, post="Windspeed") @property def state(self) -> float: @@ -359,7 +359,7 @@ class HomematicipTodayRainSensor(HomematicipGenericEntity): def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the device.""" - super().__init__(hap, device, "Today Rain") + super().__init__(hap, device, post="Today Rain") @property def state(self) -> float: diff --git a/homeassistant/components/homematicip_cloud/switch.py b/homeassistant/components/homematicip_cloud/switch.py index 64ee862b2d2..b9fbdad651a 100644 --- a/homeassistant/components/homematicip_cloud/switch.py +++ b/homeassistant/components/homematicip_cloud/switch.py @@ -54,16 +54,16 @@ async def async_setup_entry( entities.append(HomematicipSwitch(hap, device)) elif isinstance(device, AsyncOpenCollector8Module): for channel in range(1, 9): - entities.append(HomematicipMultiSwitch(hap, device, channel)) + entities.append(HomematicipMultiSwitch(hap, device, channel=channel)) elif isinstance(device, AsyncHeatingSwitch2): for channel in range(1, 3): - entities.append(HomematicipMultiSwitch(hap, device, channel)) + entities.append(HomematicipMultiSwitch(hap, device, channel=channel)) elif isinstance(device, AsyncMultiIOBox): for channel in range(1, 3): - entities.append(HomematicipMultiSwitch(hap, device, channel)) + entities.append(HomematicipMultiSwitch(hap, device, channel=channel)) elif isinstance(device, AsyncPrintedCircuitBoardSwitch2): for channel in range(1, 3): - entities.append(HomematicipMultiSwitch(hap, device, channel)) + entities.append(HomematicipMultiSwitch(hap, device, channel=channel)) for group in hap.home.groups: if isinstance(group, (AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup)): @@ -156,31 +156,17 @@ class HomematicipMultiSwitch(HomematicipGenericEntity, SwitchEntity): def __init__(self, hap: HomematicipHAP, device, channel: int) -> None: """Initialize the multi switch device.""" - self.channel = channel - super().__init__(hap, device, f"Channel{channel}") - - @property - def name(self) -> str: - """Return the name of the multi switch channel.""" - label = self._get_label_by_channel(self.channel) - if label: - return label - return super().name - - @property - def unique_id(self) -> str: - """Return a unique ID.""" - return f"{self.__class__.__name__}_{self.post}_{self._device.id}" + super().__init__(hap, device, channel=channel) @property def is_on(self) -> bool: """Return true if switch is on.""" - return self._device.functionalChannels[self.channel].on + return self._device.functionalChannels[self._channel].on async def async_turn_on(self, **kwargs) -> None: """Turn the switch on.""" - await self._device.turn_on(self.channel) + await self._device.turn_on(self._channel) async def async_turn_off(self, **kwargs) -> None: """Turn the switch off.""" - await self._device.turn_off(self.channel) + await self._device.turn_off(self._channel)