Skip to content
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
30 changes: 11 additions & 19 deletions homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@

# Loading the config flow file will register the flow
from .config_flow import get_master_gateway
from .const import (
CONF_ALLOW_CLIP_SENSOR,
CONF_ALLOW_DECONZ_GROUPS,
CONF_BRIDGEID,
CONF_MASTER_GATEWAY,
DEFAULT_PORT,
DOMAIN,
_LOGGER,
)
from .const import CONF_BRIDGEID, CONF_MASTER_GATEWAY, DEFAULT_PORT, DOMAIN, _LOGGER
from .gateway import DeconzGateway

CONFIG_SCHEMA = vol.Schema(
Expand Down Expand Up @@ -86,7 +78,7 @@ async def async_setup_entry(hass, config_entry):
hass.data[DOMAIN] = {}

if not config_entry.options:
await async_populate_options(hass, config_entry)
await async_update_master_gateway(hass, config_entry)

gateway = DeconzGateway(hass, config_entry)

Expand Down Expand Up @@ -203,25 +195,25 @@ async def async_unload_entry(hass, config_entry):
hass.services.async_remove(DOMAIN, SERVICE_DEVICE_REFRESH)

elif gateway.master:
await async_populate_options(hass, config_entry)
await async_update_master_gateway(hass, config_entry)
new_master_gateway = next(iter(hass.data[DOMAIN].values()))
await async_populate_options(hass, new_master_gateway.config_entry)
await async_update_master_gateway(hass, new_master_gateway.config_entry)

return await gateway.async_reset()


async def async_populate_options(hass, config_entry):
"""Populate default options for gateway.
async def async_update_master_gateway(hass, config_entry):
"""Update master gateway boolean.

Called by setup_entry and unload_entry.
Makes sure there is always one master available.
"""
master = not get_master_gateway(hass)

options = {
CONF_MASTER_GATEWAY: master,
CONF_ALLOW_CLIP_SENSOR: config_entry.data.get(CONF_ALLOW_CLIP_SENSOR, False),
CONF_ALLOW_DECONZ_GROUPS: config_entry.data.get(CONF_ALLOW_DECONZ_GROUPS, True),
}
old_options = dict(config_entry.options)

new_options = {CONF_MASTER_GATEWAY: master}

options = {**old_options, **new_options}

hass.config_entries.async_update_entry(config_entry, options=options)
15 changes: 8 additions & 7 deletions homeassistant/components/deconz/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
from .deconz_device import DeconzDevice
from .gateway import get_gateway_from_config_entry
from .gateway import get_gateway_from_config_entry, DeconzEntityHandler

ATTR_ORIENTATION = "orientation"
ATTR_TILTANGLE = "tiltangle"
Expand All @@ -24,24 +24,25 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the deCONZ binary sensor."""
gateway = get_gateway_from_config_entry(hass, config_entry)

entity_handler = DeconzEntityHandler(gateway)

@callback
def async_add_sensor(sensors):
"""Add binary sensor from deCONZ."""
entities = []

for sensor in sensors:

if sensor.BINARY and not (
not gateway.allow_clip_sensor and sensor.type.startswith("CLIP")
):

entities.append(DeconzBinarySensor(sensor, gateway))
if sensor.BINARY:
new_sensor = DeconzBinarySensor(sensor, gateway)
entity_handler.add_entity(new_sensor)
entities.append(new_sensor)

async_add_entities(entities, True)

gateway.listeners.append(
async_dispatcher_connect(
hass, gateway.async_event_new_device(NEW_SENSOR), async_add_sensor
hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor
)
)

Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/deconz/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ def async_add_climate(sensors):

for sensor in sensors:

if sensor.type in Thermostat.ZHATYPE and not (
not gateway.allow_clip_sensor and sensor.type.startswith("CLIP")
):

if sensor.type in Thermostat.ZHATYPE:
entities.append(DeconzThermostat(sensor, gateway))

async_add_entities(entities, True)

gateway.listeners.append(
async_dispatcher_connect(
hass, gateway.async_event_new_device(NEW_SENSOR), async_add_climate
hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_climate
)
)

Expand Down
13 changes: 9 additions & 4 deletions homeassistant/components/deconz/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Config flow to configure deCONZ component."""
import asyncio
from copy import copy

import async_timeout
import voluptuous as vol
Expand All @@ -17,6 +16,8 @@
CONF_ALLOW_CLIP_SENSOR,
CONF_ALLOW_DECONZ_GROUPS,
CONF_BRIDGEID,
DEFAULT_ALLOW_CLIP_SENSOR,
DEFAULT_ALLOW_DECONZ_GROUPS,
DEFAULT_PORT,
DOMAIN,
)
Expand Down Expand Up @@ -256,7 +257,7 @@ class DeconzOptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry):
"""Initialize deCONZ options flow."""
self.config_entry = config_entry
self.options = copy(config_entry.options)
self.options = dict(config_entry.options)

async def async_step_init(self, user_input=None):
"""Manage the deCONZ options."""
Expand All @@ -277,11 +278,15 @@ async def async_step_deconz_devices(self, user_input=None):
{
vol.Optional(
CONF_ALLOW_CLIP_SENSOR,
default=self.config_entry.options[CONF_ALLOW_CLIP_SENSOR],
default=self.config_entry.options.get(
CONF_ALLOW_CLIP_SENSOR, DEFAULT_ALLOW_CLIP_SENSOR
),
): bool,
vol.Optional(
CONF_ALLOW_DECONZ_GROUPS,
default=self.config_entry.options[CONF_ALLOW_DECONZ_GROUPS],
default=self.config_entry.options.get(
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
),
): bool,
}
),
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

DEFAULT_PORT = 80
DEFAULT_ALLOW_CLIP_SENSOR = False
DEFAULT_ALLOW_DECONZ_GROUPS = False
DEFAULT_ALLOW_DECONZ_GROUPS = True

CONF_ALLOW_CLIP_SENSOR = "allow_clip_sensor"
CONF_ALLOW_DECONZ_GROUPS = "allow_deconz_groups"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def async_add_cover(lights):

gateway.listeners.append(
async_dispatcher_connect(
hass, gateway.async_event_new_device(NEW_LIGHT), async_add_cover
hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_cover
)
)

Expand Down
27 changes: 23 additions & 4 deletions homeassistant/components/deconz/deconz_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,40 @@ def __init__(self, device, gateway):
"""Set up device and add update callback to get data from websocket."""
self._device = device
self.gateway = gateway
self.unsub_dispatcher = None
self.listeners = []

@property
def entity_registry_enabled_default(self):
"""Return if the entity should be enabled when first added to the entity registry."""
if not self.gateway.option_allow_clip_sensor and self._device.type.startswith(
"CLIP"
):
return False

if (
not self.gateway.option_allow_deconz_groups
and self._device.type == "LightGroup"
):
return False

return True

async def async_added_to_hass(self):
"""Subscribe to device events."""
self._device.register_async_callback(self.async_update_callback)
self.gateway.deconz_ids[self.entity_id] = self._device.deconz_id
self.unsub_dispatcher = async_dispatcher_connect(
self.hass, self.gateway.event_reachable, self.async_update_callback
self.listeners.append(
async_dispatcher_connect(
self.hass, self.gateway.signal_reachable, self.async_update_callback
)
)

async def async_will_remove_from_hass(self) -> None:
"""Disconnect device object when removed."""
self._device.remove_callback(self.async_update_callback)
del self.gateway.deconz_ids[self.entity_id]
self.unsub_dispatcher()
for unsub_dispatcher in self.listeners:
unsub_dispatcher()

@callback
def async_update_callback(self, force_update=False):
Expand Down
Loading