Skip to content

Commit

Permalink
Reorg channel handling for Homematic IP Cloud (home-assistant#41118)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ authored Oct 3, 2020
1 parent c00915a commit 5721225
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 73 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/homematicip_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions homeassistant/components/homematicip_cloud/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down
48 changes: 36 additions & 12 deletions homeassistant/components/homematicip_cloud/generic_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,19 @@
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)
Expand All @@ -94,6 +101,7 @@ def device_info(self) -> Dict[str, Any]:
"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
Expand Down Expand Up @@ -167,18 +175,28 @@ def _async_device_removed(self, *args, **kwargs) -> None:
@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
Expand All @@ -194,7 +212,13 @@ def available(self) -> bool:
@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]:
Expand Down
31 changes: 7 additions & 24 deletions homeassistant/components/homematicip_cloud/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ def __init__(self, hap: HomematicipHAP, device) -> None:
"""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."""
Expand Down Expand Up @@ -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],
Expand All @@ -167,7 +158,7 @@ def __init__(self, hap: HomematicipHAP, device, channel: int) -> None:

@property
def _func_channel(self) -> NotificationLightChannel:
return self._device.functionalChannels[self.channel]
return self._device.functionalChannels[self._channel]

@property
def is_on(self) -> bool:
Expand Down Expand Up @@ -198,14 +189,6 @@ def device_state_attributes(self) -> Dict[str, Any]:

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."""
Expand All @@ -214,7 +197,7 @@ def supported_features(self) -> int:
@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."""
Expand All @@ -237,7 +220,7 @@ async def async_turn_on(self, **kwargs) -> None:
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,
Expand All @@ -250,7 +233,7 @@ async def async_turn_off(self, **kwargs) -> None:
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,
Expand Down
16 changes: 8 additions & 8 deletions homeassistant/components/homematicip_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 8 additions & 22 deletions homeassistant/components/homematicip_cloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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)

0 comments on commit 5721225

Please sign in to comment.