1 """Support for Telldus Live."""
4 from functools
import partial
7 from tellduslive
import DIM, TURNON, UP, Session
8 import voluptuous
as vol
10 from homeassistant
import config_entries
28 TELLDUS_DISCOVERY_NEW,
31 APPLICATION_NAME =
"Home Assistant"
33 _LOGGER = logging.getLogger(__name__)
35 CONFIG_SCHEMA = vol.Schema(
39 vol.Optional(CONF_HOST, default=DOMAIN): cv.string,
40 vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): vol.All(
41 cv.time_period, vol.Clamp(min=MIN_UPDATE_INTERVAL)
46 extra=vol.ALLOW_EXTRA,
49 DATA_CONFIG_ENTRY_LOCK =
"tellduslive_config_entry_lock"
50 CONFIG_ENTRY_IS_SETUP =
"telldus_config_entry_is_setup"
52 NEW_CLIENT_TASK =
"telldus_new_client_task"
53 INTERVAL_TRACKER = f
"{DOMAIN}_INTERVAL"
57 """Create a tellduslive session."""
58 conf = entry.data[KEY_SESSION]
63 session = await hass.async_add_executor_job(partial(Session, **conf))
66 PUBLIC_KEY, NOT_SO_PRIVATE_KEY, application=APPLICATION_NAME, **conf
69 if not session.is_authorized:
70 _LOGGER.error(
"Authentication Error")
73 hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
74 hass.data[CONFIG_ENTRY_IS_SETUP] = set()
75 hass.data[NEW_CLIENT_TASK] = hass.loop.create_task(
83 """Add the hubs associated with the current client to device_registry."""
84 interval = entry.data[KEY_SCAN_INTERVAL]
85 _LOGGER.debug(
"Update interval %s seconds", interval)
87 hass.data[DOMAIN] = client
88 dev_reg = dr.async_get(hass)
89 for hub
in await client.async_get_hubs():
90 _LOGGER.debug(
"Connected hub %s", hub[
"name"])
91 dev_reg.async_get_or_create(
92 config_entry_id=entry.entry_id,
93 identifiers={(DOMAIN, hub[
"id"])},
94 manufacturer=
"Telldus",
97 sw_version=hub[
"version"],
102 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
103 """Set up the Telldus Live component."""
104 if DOMAIN
not in config:
107 hass.async_create_task(
108 hass.config_entries.flow.async_init(
110 context={
"source": config_entries.SOURCE_IMPORT},
112 CONF_HOST: config[DOMAIN].
get(CONF_HOST),
113 KEY_SCAN_INTERVAL: config[DOMAIN][CONF_SCAN_INTERVAL],
121 """Unload a config entry."""
122 if not hass.data[NEW_CLIENT_TASK].done():
123 hass.data[NEW_CLIENT_TASK].cancel()
124 interval_tracker = hass.data.pop(INTERVAL_TRACKER)
126 unload_ok = await hass.config_entries.async_unload_platforms(
127 config_entry, CONFIG_ENTRY_IS_SETUP
129 del hass.data[DOMAIN]
130 del hass.data[DATA_CONFIG_ENTRY_LOCK]
131 del hass.data[CONFIG_ENTRY_IS_SETUP]
136 """Get the latest data and update the states."""
138 def __init__(self, hass, config_entry, session, interval):
139 """Initialize the Tellus data object."""
149 """Return hubs registered for the user."""
150 clients = await self.
_hass_hass.async_add_executor_job(self.
_client_client.get_clients)
154 """Return device info."""
159 """Find out what type of HA component to create."""
163 if device.methods & DIM:
165 if device.methods & UP:
167 if device.methods & TURNON:
169 if device.methods == 0:
170 return "binary_sensor"
171 _LOGGER.warning(
"Unidentified device type (methods: %d)", device.methods)
175 """Discover the component."""
179 {device_id: await self.
_hass_hass.async_add_executor_job(device.info)}
181 async
with self.
_hass_hass.data[DATA_CONFIG_ENTRY_LOCK]:
182 if component
not in self.
_hass_hass.data[CONFIG_ENTRY_IS_SETUP]:
183 await self.
_hass_hass.config_entries.async_forward_entry_setups(
186 self.
_hass_hass.data[CONFIG_ENTRY_IS_SETUP].
add(component)
190 (device.device_id, item.name, item.scale)
for item
in device.items
193 device_ids.append(device_id)
194 for _id
in device_ids:
196 self.
_hass_hass, TELLDUS_DISCOVERY_NEW.format(component, DOMAIN), _id
200 """Periodically poll the servers for current state."""
202 if not await self.
_hass_hass.async_add_executor_job(self.
_client_client.update):
203 _LOGGER.warning(
"Failed request")
205 dev_ids = {dev.device_id
for dev
in self.
_client_client.devices}
208 for d_id
in new_devices:
218 """Return device representation."""
222 """Return device availability."""
223 return device_id
in self.
_client_client.device_ids
def __init__(self, hass, config_entry, session, interval)
def identify_device(device)
def device_info(self, device_id)
def device(self, device_id)
def _discover(self, device_id)
def is_available(self, device_id)
bool add(self, _T matcher)
web.Response get(self, web.Request request, str config_key)
def async_new_client(hass, session, entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup(HomeAssistant hass, ConfigType config)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)