Add reset_energy_counter service to Homematic IP Cloud (#30256)

* Add reset_energy_counter service to Homematic IP Cloud

* Fix isort

* Register service as admin service
This commit is contained in:
SukramJ 2020-01-07 16:24:46 +01:00 committed by Charles Garwood
parent 1d1aa06d05
commit a4c830b5e4
3 changed files with 63 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import logging
from pathlib import Path
from typing import Optional
from homematicip.aio.device import AsyncSwitchMeasuring
from homematicip.aio.group import AsyncHeatingGroup
from homematicip.aio.home import AsyncHome
from homematicip.base.helpers import handle_config
@ -47,6 +48,7 @@ SERVICE_ACTIVATE_VACATION = "activate_vacation"
SERVICE_DEACTIVATE_ECO_MODE = "deactivate_eco_mode"
SERVICE_DEACTIVATE_VACATION = "deactivate_vacation"
SERVICE_DUMP_HAP_CONFIG = "dump_hap_config"
SERVICE_RESET_ENERGY_COUNTER = "reset_energy_counter"
SERVICE_SET_ACTIVE_CLIMATE_PROFILE = "set_active_climate_profile"
CONFIG_SCHEMA = vol.Schema(
@ -116,6 +118,10 @@ SCHEMA_DUMP_HAP_CONFIG = vol.Schema(
}
)
SCHEMA_RESET_ENERGY_COUNTER = vol.Schema(
{vol.Required(ATTR_ENTITY_ID): comp_entity_ids}
)
async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
"""Set up the HomematicIP Cloud component."""
@ -245,7 +251,7 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
if entity_id_list != "all":
for entity_id in entity_id_list:
group = hap.hmip_device_by_entity_id.get(entity_id)
if group:
if group and isinstance(group, AsyncHeatingGroup):
await group.set_active_profile(climate_profile_index)
else:
for group in hap.home.groups:
@ -289,6 +295,28 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
schema=SCHEMA_DUMP_HAP_CONFIG,
)
async def _async_reset_energy_counter(service):
"""Service to reset the energy counter."""
entity_id_list = service.data[ATTR_ENTITY_ID]
for hap in hass.data[DOMAIN].values():
if entity_id_list != "all":
for entity_id in entity_id_list:
device = hap.hmip_device_by_entity_id.get(entity_id)
if device and isinstance(device, AsyncSwitchMeasuring):
await device.reset_energy_counter()
else:
for device in hap.home.devices:
if isinstance(device, AsyncSwitchMeasuring):
await device.reset_energy_counter()
hass.helpers.service.async_register_admin_service(
DOMAIN,
SERVICE_RESET_ENERGY_COUNTER,
_async_reset_energy_counter,
schema=SCHEMA_RESET_ENERGY_COUNTER,
)
def _get_home(hapid: str) -> Optional[AsyncHome]:
"""Return a HmIP home."""
hap = hass.data[DOMAIN].get(hapid)

View File

@ -69,3 +69,10 @@ dump_hap_config:
anonymize:
description: (Default is True) Should the Configuration be anonymized?
example: True
reset_energy_counter:
description: Reset the energy counter of a measuring entity.
fields:
entity_id:
description: The ID of the measuring entity. Use 'all' keyword to reset all energy counters.
example: switch.livingroom

View File

@ -130,3 +130,30 @@ async def test_hap_with_name(hass, mock_connection, hmip_config_entry):
assert hmip_device
assert ha_state.state == STATE_ON
assert ha_state.attributes["friendly_name"] == entity_name
async def test_hmip_reset_energy_counter_services(hass, mock_hap_with_service):
"""Test reset_energy_counter service."""
entity_id = "switch.pc"
entity_name = "Pc"
device_model = "HMIP-PSM"
ha_state, hmip_device = get_and_check_entity_basics(
hass, mock_hap_with_service, entity_id, entity_name, device_model
)
assert ha_state
await hass.services.async_call(
"homematicip_cloud",
"reset_energy_counter",
{"entity_id": "switch.pc"},
blocking=True,
)
assert hmip_device.mock_calls[-1][0] == "reset_energy_counter"
assert len(hmip_device._connection.mock_calls) == 2 # pylint: disable=W0212
await hass.services.async_call(
"homematicip_cloud", "reset_energy_counter", {"entity_id": "all"}, blocking=True
)
assert hmip_device.mock_calls[-1][0] == "reset_energy_counter"
assert len(hmip_device._connection.mock_calls) == 12 # pylint: disable=W0212