Skip to content

Commit

Permalink
Add binary_sensor for cloud connectivity to HomematicIP Cloud (home-a…
Browse files Browse the repository at this point in the history
…ssistant#39675)

* Add binary_sensor for cloud connectivity to HomematicIP Cloud

* Fix Test

* Remove device_class

* Switch from _device to _home

* Switch from _device to _home for sensor
  • Loading branch information
SukramJ authored Sep 19, 2020
1 parent b088830 commit e300cf3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
40 changes: 39 additions & 1 deletion homeassistant/components/homematicip_cloud/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def async_setup_entry(
) -> None:
"""Set up the HomematicIP Cloud binary sensor from a config entry."""
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
entities = []
entities = [HomematicipCloudConnectionSensor(hap)]
for device in hap.home.devices:
if isinstance(device, AsyncAccelerationSensor):
entities.append(HomematicipAccelerationSensor(hap, device))
Expand Down Expand Up @@ -136,6 +136,44 @@ async def async_setup_entry(
async_add_entities(entities)


class HomematicipCloudConnectionSensor(HomematicipGenericEntity, BinarySensorEntity):
"""Representation of the HomematicIP cloud connection sensor."""

def __init__(self, hap: HomematicipHAP) -> None:
"""Initialize the cloud connection sensor."""
super().__init__(hap, hap.home, "Cloud Connection")

@property
def device_info(self) -> Dict[str, Any]:
"""Return device specific attributes."""
# Adds a sensor to the existing HAP device
return {
"identifiers": {
# Serial numbers of Homematic IP device
(HMIPC_DOMAIN, self._home.id)
}
}

@property
def icon(self) -> str:
"""Return the icon of the access point entity."""
return (
"mdi:access-point-network"
if self._home.connected
else "mdi:access-point-network-off"
)

@property
def is_on(self) -> bool:
"""Return true if hap is connected to cloud."""
return self._home.connected

@property
def available(self) -> bool:
"""Sensor is always available."""
return True


class HomematicipBaseActionSensor(HomematicipGenericEntity, BinarySensorEntity):
"""Representation of the HomematicIP base action sensor."""

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homematicip_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class HomematicipAccesspointStatus(HomematicipGenericEntity):

def __init__(self, hap: HomematicipHAP) -> None:
"""Initialize access point status entity."""
super().__init__(hap, hap.home)
super().__init__(hap, hap.home, "Duty Cycle")

@property
def device_info(self) -> Dict[str, Any]:
Expand All @@ -134,7 +134,7 @@ def device_info(self) -> Dict[str, Any]:
return {
"identifiers": {
# Serial numbers of Homematic IP device
(HMIPC_DOMAIN, self._device.id)
(HMIPC_DOMAIN, self._home.id)
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/components/homematicip_cloud/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ async def test_manually_configured_platform(hass):
assert not hass.data.get(HMIPC_DOMAIN)


async def test_hmip_access_point_cloud_connection_sensor(
hass, default_mock_hap_factory
):
"""Test HomematicipCloudConnectionSensor."""
entity_id = "binary_sensor.access_point_cloud_connection"
entity_name = "Access Point Cloud Connection"
device_model = None
mock_hap = await default_mock_hap_factory.async_get_mock_hap(
test_devices=[entity_name]
)

ha_state, hmip_device = get_and_check_entity_basics(
hass, mock_hap, entity_id, entity_name, device_model
)

assert ha_state.state == STATE_ON

await async_manipulate_test_data(hass, hmip_device, "connected", False)

ha_state = hass.states.get(entity_id)
assert ha_state.state == STATE_OFF


async def test_hmip_acceleration_sensor(hass, default_mock_hap_factory):
"""Test HomematicipAccelerationSensor."""
entity_id = "binary_sensor.garagentor"
Expand Down
2 changes: 1 addition & 1 deletion tests/components/homematicip_cloud/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def test_hmip_load_all_supported_devices(hass, default_mock_hap_factory):
test_devices=None, test_groups=None
)

assert len(mock_hap.hmip_device_by_entity_id) == 191
assert len(mock_hap.hmip_device_by_entity_id) == 192


async def test_hmip_remove_device(hass, default_mock_hap_factory):
Expand Down
4 changes: 2 additions & 2 deletions tests/components/homematicip_cloud/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async def test_manually_configured_platform(hass):

async def test_hmip_accesspoint_status(hass, default_mock_hap_factory):
"""Test HomematicipSwitch."""
entity_id = "sensor.access_point"
entity_name = "Access Point"
entity_id = "sensor.access_point_duty_cycle"
entity_name = "Access Point Duty Cycle"
device_model = None
mock_hap = await default_mock_hap_factory.async_get_mock_hap(
test_devices=[entity_name]
Expand Down

0 comments on commit e300cf3

Please sign in to comment.