mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Allow unloading of LIFX config entry (#18535)
This commit is contained in:
parent
796933de68
commit
b066877453
@ -8,7 +8,7 @@ from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|||||||
|
|
||||||
|
|
||||||
DOMAIN = 'lifx'
|
DOMAIN = 'lifx'
|
||||||
REQUIREMENTS = ['aiolifx==0.6.5']
|
REQUIREMENTS = ['aiolifx==0.6.6']
|
||||||
|
|
||||||
CONF_SERVER = 'server'
|
CONF_SERVER = 'server'
|
||||||
CONF_BROADCAST = 'broadcast'
|
CONF_BROADCAST = 'broadcast'
|
||||||
@ -25,6 +25,8 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
}
|
}
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
DATA_LIFX_MANAGER = 'lifx_manager'
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Set up the LIFX component."""
|
"""Set up the LIFX component."""
|
||||||
@ -43,6 +45,16 @@ async def async_setup_entry(hass, entry):
|
|||||||
"""Set up LIFX from a config entry."""
|
"""Set up LIFX from a config entry."""
|
||||||
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
|
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
|
||||||
entry, LIGHT_DOMAIN))
|
entry, LIGHT_DOMAIN))
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def async_unload_entry(hass, entry):
|
||||||
|
"""Unload a config entry."""
|
||||||
|
hass.data.pop(DATA_LIFX_MANAGER).cleanup()
|
||||||
|
|
||||||
|
await hass.config_entries.async_forward_entry_unload(entry, LIGHT_DOMAIN)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ from homeassistant.components.light import (
|
|||||||
SUPPORT_TRANSITION, VALID_BRIGHTNESS, VALID_BRIGHTNESS_PCT, Light,
|
SUPPORT_TRANSITION, VALID_BRIGHTNESS, VALID_BRIGHTNESS_PCT, Light,
|
||||||
preprocess_turn_on_alternatives)
|
preprocess_turn_on_alternatives)
|
||||||
from homeassistant.components.lifx import (
|
from homeassistant.components.lifx import (
|
||||||
DOMAIN as LIFX_DOMAIN, CONF_SERVER, CONF_BROADCAST)
|
DOMAIN as LIFX_DOMAIN, DATA_LIFX_MANAGER, CONF_SERVER, CONF_BROADCAST)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -155,27 +155,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
interfaces = [{}]
|
interfaces = [{}]
|
||||||
|
|
||||||
lifx_manager = LIFXManager(hass, async_add_entities)
|
lifx_manager = LIFXManager(hass, async_add_entities)
|
||||||
|
hass.data[DATA_LIFX_MANAGER] = lifx_manager
|
||||||
|
|
||||||
for interface in interfaces:
|
for interface in interfaces:
|
||||||
kwargs = {'discovery_interval': DISCOVERY_INTERVAL}
|
lifx_manager.start_discovery(interface)
|
||||||
broadcast_ip = interface.get(CONF_BROADCAST)
|
|
||||||
if broadcast_ip:
|
|
||||||
kwargs['broadcast_ip'] = broadcast_ip
|
|
||||||
lifx_discovery = aiolifx().LifxDiscovery(
|
|
||||||
hass.loop, lifx_manager, **kwargs)
|
|
||||||
|
|
||||||
kwargs = {}
|
|
||||||
listen_ip = interface.get(CONF_SERVER)
|
|
||||||
if listen_ip:
|
|
||||||
kwargs['listen_ip'] = listen_ip
|
|
||||||
lifx_discovery.start(**kwargs)
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def cleanup(event):
|
|
||||||
"""Clean up resources."""
|
|
||||||
lifx_discovery.cleanup()
|
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -226,10 +209,43 @@ class LIFXManager:
|
|||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.async_add_entities = async_add_entities
|
self.async_add_entities = async_add_entities
|
||||||
self.effects_conductor = aiolifx_effects().Conductor(loop=hass.loop)
|
self.effects_conductor = aiolifx_effects().Conductor(loop=hass.loop)
|
||||||
|
self.discoveries = []
|
||||||
|
self.cleanup_unsub = self.hass.bus.async_listen(
|
||||||
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
|
self.cleanup)
|
||||||
|
|
||||||
self.register_set_state()
|
self.register_set_state()
|
||||||
self.register_effects()
|
self.register_effects()
|
||||||
|
|
||||||
|
def start_discovery(self, interface):
|
||||||
|
"""Start discovery on a network interface."""
|
||||||
|
kwargs = {'discovery_interval': DISCOVERY_INTERVAL}
|
||||||
|
broadcast_ip = interface.get(CONF_BROADCAST)
|
||||||
|
if broadcast_ip:
|
||||||
|
kwargs['broadcast_ip'] = broadcast_ip
|
||||||
|
lifx_discovery = aiolifx().LifxDiscovery(
|
||||||
|
self.hass.loop, self, **kwargs)
|
||||||
|
|
||||||
|
kwargs = {}
|
||||||
|
listen_ip = interface.get(CONF_SERVER)
|
||||||
|
if listen_ip:
|
||||||
|
kwargs['listen_ip'] = listen_ip
|
||||||
|
lifx_discovery.start(**kwargs)
|
||||||
|
|
||||||
|
self.discoveries.append(lifx_discovery)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def cleanup(self, event=None):
|
||||||
|
"""Release resources."""
|
||||||
|
self.cleanup_unsub()
|
||||||
|
|
||||||
|
for discovery in self.discoveries:
|
||||||
|
discovery.cleanup()
|
||||||
|
|
||||||
|
for service in [SERVICE_LIFX_SET_STATE, SERVICE_EFFECT_STOP,
|
||||||
|
SERVICE_EFFECT_PULSE, SERVICE_EFFECT_COLORLOOP]:
|
||||||
|
self.hass.services.async_remove(DOMAIN, service)
|
||||||
|
|
||||||
def register_set_state(self):
|
def register_set_state(self):
|
||||||
"""Register the LIFX set_state service call."""
|
"""Register the LIFX set_state service call."""
|
||||||
async def service_handler(service):
|
async def service_handler(service):
|
||||||
|
@ -111,7 +111,7 @@ aiohue==1.5.0
|
|||||||
aioimaplib==0.7.13
|
aioimaplib==0.7.13
|
||||||
|
|
||||||
# homeassistant.components.lifx
|
# homeassistant.components.lifx
|
||||||
aiolifx==0.6.5
|
aiolifx==0.6.6
|
||||||
|
|
||||||
# homeassistant.components.light.lifx
|
# homeassistant.components.light.lifx
|
||||||
aiolifx_effects==0.2.1
|
aiolifx_effects==0.2.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user