Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gateways to Device Registry #333

Merged
merged 19 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Custom component for Home Assistant to interact with smart devices via the Somfy TaHoma platform. Despite the name of this integration, many other platforms sharing the same OverKiz API structure are supported as well. Have a look at all [supported hubs](#supported-hubs).

>This component builds upon the work of [@philklei](https://github.com/philklei) and is an updated version of the [original Tahoma integration](https://www.home-assistant.io/integrations/tahoma/) in Home Assistant with the goal of eventually merging into core. The installation of this component will replace the original TaHoma integration and thus allows you to beta-test [all changes](https://github.com/iMicknl/ha-tahoma/releases.md).
>This component builds upon the work of [@philklei](https://github.com/philklei) and is an updated version of the [original Tahoma integration](https://www.home-assistant.io/integrations/tahoma/) in Home Assistant with the goal of eventually merging into core. The installation of this component will replace the original TaHoma integration and thus allows you to beta-test [all changes](https://github.com/iMicknl/ha-tahoma/releases).

## Supported hubs

Expand Down Expand Up @@ -59,4 +59,4 @@ The previous version had functionality to exclude devices from Home Assistant. S

### Retrieve HomeKit code

If your TaHoma box supports HomeKit natively, the integration will log the HomeKit code on start currently. This is currently an experimental implementation and in order to enable this, you need to have debug logging enabled.
If your TaHoma box supports HomeKit natively, the integration will log the HomeKit code on start currently. This is currently an experimental implementation and in order to enable this, you need to have debug logging enabled.
47 changes: 44 additions & 3 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
from collections import defaultdict
from datetime import timedelta
from enum import Enum
import logging

from aiohttp import ClientError, ServerDisconnectedError
Expand All @@ -10,7 +11,11 @@
from homeassistant.const import CONF_EXCLUDE, CONF_PASSWORD, CONF_SOURCE, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv, service
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
service,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from pyhoma.client import TahomaClient
from pyhoma.exceptions import (
Expand Down Expand Up @@ -104,6 +109,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await client.login()
devices = await client.get_devices()
scenarios = await client.get_scenarios()
gateways = await client.get_gateways()
except BadCredentialsException:
_LOGGER.error("invalid_auth")
return False
Expand Down Expand Up @@ -147,7 +153,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if platform:
entities[platform].append(device)
_LOGGER.debug(
"Added TaHoma device (%s - %s - %s - %s)",
"Added device (%s - %s - %s - %s)",
device.controllable_name,
device.ui_class,
device.widget,
Expand All @@ -158,7 +164,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
and device.ui_class not in IGNORED_TAHOMA_TYPES
):
_LOGGER.debug(
"Unsupported TaHoma device detected (%s - %s - %s - %s)",
"Unsupported device detected (%s - %s - %s - %s)",
device.controllable_name,
device.ui_class,
device.widget,
Expand Down Expand Up @@ -199,6 +205,36 @@ async def handle_execute_command(call):
),
)

device_registry = await dr.async_get_registry(hass)

for gateway in gateways:
_LOGGER.debug(
"Added gateway (%s - %s - %s)",
gateway.id,
gateway.type,
gateway.sub_type,
)

gateway_model = (
beautify_name(gateway.sub_type.name)
if isinstance(gateway.sub_type, Enum)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be something else than an Enum?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it can be something else... (https://github.com/iMicknl/python-tahoma-api/blob/78587f4f9547b2621a1123817efb2f2a66f21d9b/pyhoma/models.py#L394-L402).

However, maybe we should address it there. And throw a None + warning that we don't have the value in Enum.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Pyhoma the code looks good. But here it sounds weird :S

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also do this in beautify_name(), but I would prefer to just pass a string to that to keep is reusable.

else None
)
gateway_name = (
f"{beautify_name(gateway.type.name)} hub"
if isinstance(gateway.type, Enum)
else None
)

device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, gateway.id)},
model=gateway_model,
manufacturer="Somfy",
name=gateway_name,
sw_version=gateway.connectivity.protocol_version,
)

return True


Expand Down Expand Up @@ -240,3 +276,8 @@ def print_homekit_setup_code(device: Device):

if homekit:
_LOGGER.info("HomeKit support detected with setup code %s.", homekit.value)


def beautify_name(name: str):
"""Return human readable string."""
return name.replace("_", " ").title()
2 changes: 1 addition & 1 deletion custom_components/tahoma/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/tahoma",
"requirements": [
"pyhoma==0.5.1"
"pyhoma==0.5.4"
],
"codeowners": [
"@philklei",
Expand Down
11 changes: 11 additions & 0 deletions custom_components/tahoma/tahoma_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Parent class for every TaHoma device."""
import logging
import re
from typing import Any, Dict, Optional

from homeassistant.const import ATTR_BATTERY_LEVEL
Expand Down Expand Up @@ -115,6 +116,7 @@ def device_info(self) -> Dict[str, Any]:
"name": self.name,
"model": model,
"sw_version": self.device.controllable_name,
"via_device": self.get_gateway_id(self.device_url),
}

def select_command(self, *commands: str) -> Optional[str]:
Expand Down Expand Up @@ -166,3 +168,12 @@ async def async_execute_command(self, command_name: str, *args: Any):
async def async_cancel_command(self, exec_id: str):
"""Cancel device command in async context."""
await self.coordinator.client.cancel_command(exec_id)

def get_gateway_id(self, device_url: str):
"""Retrieve gateway id from device url."""
result = re.search(r":\/\/(.*)\/", device_url)

if result:
return result.group(1)
else:
return None
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pytest-cov<3.0.0
pytest-homeassistant

# from our manifest.json for our Custom Component
pyhoma==0.5.1
pyhoma==0.5.4