hass-core/homeassistant/components/mysensors/light.py
functionpointer c01e01f797
MySensors config flow (#45421)
* MySensors: Add type annotations

Adds a bunch of type annotations that were created
while understanding the code.

* MySensors: Change GatewayId to string

In preparation for config flow.
The GatewayId used to be id(gateway).

With config flows, every gateway will have its own
ConfigEntry. Every ConfigEntry has a unique id.
Thus we would have two separate but one-to-one related ID systems.

This commit removes this unneeded duplication by using the id of the ConfigEntry
as GatewayId.

* MySensors: Add unique_id to all entities

This allows entities to work well with the frontend.

* MySensors: Add device_info to all entities

Entities belonging to the same node_id will now by grouped as a device.

* MySensors: clean up device.py a bit

* MySensors: Add config flow support

With this change the MySensors can be fully configured from the GUI.

Legacy configuration.yaml configs will be migrated by reading them once.
Note that custom node names are not migrated. Users will have to re-enter
the names in the front-end.
Since there is no straight-forward way to configure global settings,
all previously global settings are now per-gateway. These settings include:
- MQTT retain
- optimistic
- persistence enable
- MySensors version

When a MySensors integration is loaded, it works as follows:
1. __init__.async_setup_entry is called
2. for every platform, async_forward_entry_setup is called
3. the platform's async_setup_entry is called
4. __init__.setup_mysensors_platform is called
5. the entity's constructor (e.g. MySensorsCover) is called
6. the created entity is stored in a dict in the hass object

* MySensors: Fix linter errors

* MySensors: Remove unused import

* MySensors: Feedback from @MartinHjelmare

* MySensors: Multi-step config flow

* MySensors: More feedback

* MySensors: Move all storage in hass object under DOMAIN

The integration now stores everything under hass.data["mysensors"]
instead of using several top level keys.

* MySensors: await shutdown of gateway instead of creating a task

* MySensors: Rename Ethernet to TCP

* MySensors: Absolute imports and cosmetic changes

* MySensors: fix gw_stop

* MySensors: Allow user to specify persistence file

* MySensors: Nicer log message

* MySensors: Add lots of unit tests

* MySensors: Fix legacy import of persistence file name

Turns out tests help to find bugs :D

* MySensors: Improve test coverage

* MySensors: Use json persistence files by default

* MySensors: Code style improvements

* MySensors: Stop adding attributes to existing objects

This commit removes the extra attributes that were being
added to the gateway objects from pymysensors.

Most attributes were easy to remove, except for the gateway id.
The MySensorsDevice class needs the gateway id as it is part of its DevId
as well as the unique_id and device_info.
Most MySensorsDevices actually end up being Entities.
Entities have access to their ConfigEntry via self.platform.config_entry.

However, the device_tracker platform does not become an Entity.
For this reason, the gateway id is not fetched from self.plaform but
given as an argument.

Additionally, MySensorsDevices expose the address of the gateway
(CONF_DEVICE). Entities can easily fetch this information via self.platform,
but the device_tracker cannot. This commit chooses to remove the gateway
address from device_tracker. While this could in theory break some automations,
the simplicity of this solution was deemed worth it.
The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices
is not viable, because device_tracker is initialized by the async_setup_scanner function
that isn't supplied a ConfigEntry. It only gets discovery_info.
Adding the entire ConfigEntry doesn't seem appropriate for this edge case.

* MySensors: Fix gw_stop and the translations

* MySensors: Fix incorrect function calls

* MySensors: Fewer comments in const.py

* MySensors: Remove union from _get_gateway and remove id from try_connect

* MySensors: Deprecate nodes option in configuration.yaml

* MySensors: Use version parser from packaging

* MySensors: Remove prefix from unique_id and change some private property names

* MySensors: Change _get_gateway function signature

* MySensors: add packaging==20.8 for the version parser

* MySensors: Rename some stuff

* MySensors: use pytest.mark.parametrize

* MySensors: Clean up test cases

* MySensors: Remove unneeded parameter from devices

* Revert "MySensors: add packaging==20.8 for the version parser"

This reverts commit 6b200ee01a.

* MySensors: Use core interface for testing configuration.yaml import

* MySensors: Fix test_init

* MySensors: Rename a few variables

* MySensors: cosmetic changes

* MySensors: Update strings.json

* MySensors: Still more feedback from @MartinHjelmare

* MySensors: Remove unused strings

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* MySensors: Fix typo and remove another unused string

* MySensors: More strings.json

* MySensors: Fix gateway ready handler

* MySensors: Add duplicate detection to config flows

* MySensors: Deal with non-existing topics and ports.

Includes unit tests for these cases.

* MySensors: Use awesomeversion instead of packaging

* Add string already_configured

* MySensors: Abort config flow when config is found to be invalid while importing

* MySensors: Copy all error messages to also be abort messages

All error strings may now also be used as an abort reason,
so the strings should be defined

* Use string references

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00

262 lines
8.5 KiB
Python

"""Support for MySensors lights."""
from typing import Callable
from homeassistant.components import mysensors
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_HS_COLOR,
ATTR_WHITE_VALUE,
DOMAIN,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_WHITE_VALUE,
LightEntity,
)
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType
import homeassistant.util.color as color_util
from homeassistant.util.color import rgb_hex_to_rgb_list
SUPPORT_MYSENSORS_RGBW = SUPPORT_COLOR | SUPPORT_WHITE_VALUE
async def async_setup_entry(
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities: Callable
):
"""Set up this platform for a specific ConfigEntry(==Gateway)."""
device_class_map = {
"S_DIMMER": MySensorsLightDimmer,
"S_RGB_LIGHT": MySensorsLightRGB,
"S_RGBW_LIGHT": MySensorsLightRGBW,
}
async def async_discover(discovery_info):
"""Discover and add a MySensors light."""
mysensors.setup_mysensors_platform(
hass,
DOMAIN,
discovery_info,
device_class_map,
async_add_entities=async_add_entities,
)
await on_unload(
hass,
config_entry,
async_dispatcher_connect(
hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
async_discover,
),
)
class MySensorsLight(mysensors.device.MySensorsEntity, LightEntity):
"""Representation of a MySensors Light child node."""
def __init__(self, *args):
"""Initialize a MySensors Light."""
super().__init__(*args)
self._state = None
self._brightness = None
self._hs = None
self._white = None
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def hs_color(self):
"""Return the hs color value [int, int]."""
return self._hs
@property
def white_value(self):
"""Return the white value of this light between 0..255."""
return self._white
@property
def is_on(self):
"""Return true if device is on."""
return self._state
def _turn_on_light(self):
"""Turn on light child device."""
set_req = self.gateway.const.SetReq
if self._state:
return
self.gateway.set_child_value(
self.node_id, self.child_id, set_req.V_LIGHT, 1, ack=1
)
if self.assumed_state:
# optimistically assume that light has changed state
self._state = True
self._values[set_req.V_LIGHT] = STATE_ON
def _turn_on_dimmer(self, **kwargs):
"""Turn on dimmer child device."""
set_req = self.gateway.const.SetReq
brightness = self._brightness
if (
ATTR_BRIGHTNESS not in kwargs
or kwargs[ATTR_BRIGHTNESS] == self._brightness
or set_req.V_DIMMER not in self._values
):
return
brightness = kwargs[ATTR_BRIGHTNESS]
percent = round(100 * brightness / 255)
self.gateway.set_child_value(
self.node_id, self.child_id, set_req.V_DIMMER, percent, ack=1
)
if self.assumed_state:
# optimistically assume that light has changed state
self._brightness = brightness
self._values[set_req.V_DIMMER] = percent
def _turn_on_rgb_and_w(self, hex_template, **kwargs):
"""Turn on RGB or RGBW child device."""
rgb = list(color_util.color_hs_to_RGB(*self._hs))
white = self._white
hex_color = self._values.get(self.value_type)
hs_color = kwargs.get(ATTR_HS_COLOR)
if hs_color is not None:
new_rgb = color_util.color_hs_to_RGB(*hs_color)
else:
new_rgb = None
new_white = kwargs.get(ATTR_WHITE_VALUE)
if new_rgb is None and new_white is None:
return
if new_rgb is not None:
rgb = list(new_rgb)
if hex_template == "%02x%02x%02x%02x":
if new_white is not None:
rgb.append(new_white)
else:
rgb.append(white)
hex_color = hex_template % tuple(rgb)
if len(rgb) > 3:
white = rgb.pop()
self.gateway.set_child_value(
self.node_id, self.child_id, self.value_type, hex_color, ack=1
)
if self.assumed_state:
# optimistically assume that light has changed state
self._hs = color_util.color_RGB_to_hs(*rgb)
self._white = white
self._values[self.value_type] = hex_color
async def async_turn_off(self, **kwargs):
"""Turn the device off."""
value_type = self.gateway.const.SetReq.V_LIGHT
self.gateway.set_child_value(self.node_id, self.child_id, value_type, 0, ack=1)
if self.assumed_state:
# optimistically assume that light has changed state
self._state = False
self._values[value_type] = STATE_OFF
self.async_write_ha_state()
@callback
def _async_update_light(self):
"""Update the controller with values from light child."""
value_type = self.gateway.const.SetReq.V_LIGHT
self._state = self._values[value_type] == STATE_ON
@callback
def _async_update_dimmer(self):
"""Update the controller with values from dimmer child."""
value_type = self.gateway.const.SetReq.V_DIMMER
if value_type in self._values:
self._brightness = round(255 * int(self._values[value_type]) / 100)
if self._brightness == 0:
self._state = False
@callback
def _async_update_rgb_or_w(self):
"""Update the controller with values from RGB or RGBW child."""
value = self._values[self.value_type]
color_list = rgb_hex_to_rgb_list(value)
if len(color_list) > 3:
self._white = color_list.pop()
self._hs = color_util.color_RGB_to_hs(*color_list)
class MySensorsLightDimmer(MySensorsLight):
"""Dimmer child class to MySensorsLight."""
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
self._turn_on_light()
self._turn_on_dimmer(**kwargs)
if self.assumed_state:
self.async_write_ha_state()
async def async_update(self):
"""Update the controller with the latest value from a sensor."""
await super().async_update()
self._async_update_light()
self._async_update_dimmer()
class MySensorsLightRGB(MySensorsLight):
"""RGB child class to MySensorsLight."""
@property
def supported_features(self):
"""Flag supported features."""
set_req = self.gateway.const.SetReq
if set_req.V_DIMMER in self._values:
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
return SUPPORT_COLOR
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
self._turn_on_light()
self._turn_on_dimmer(**kwargs)
self._turn_on_rgb_and_w("%02x%02x%02x", **kwargs)
if self.assumed_state:
self.async_write_ha_state()
async def async_update(self):
"""Update the controller with the latest value from a sensor."""
await super().async_update()
self._async_update_light()
self._async_update_dimmer()
self._async_update_rgb_or_w()
class MySensorsLightRGBW(MySensorsLightRGB):
"""RGBW child class to MySensorsLightRGB."""
@property
def supported_features(self):
"""Flag supported features."""
set_req = self.gateway.const.SetReq
if set_req.V_DIMMER in self._values:
return SUPPORT_BRIGHTNESS | SUPPORT_MYSENSORS_RGBW
return SUPPORT_MYSENSORS_RGBW
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
self._turn_on_light()
self._turn_on_dimmer(**kwargs)
self._turn_on_rgb_and_w("%02x%02x%02x%02x", **kwargs)
if self.assumed_state:
self.async_write_ha_state()