jduquennoy af6a4bb6cf
Refactor Enocean part 1 (#35927)
* First step of an EnOcean integration refactoring, including code reorganisation and support of a setup config flow

* Moved title to root of strings file

* Fixed pre-commit checks failures

* Fixed linter errors

* Updated formatted string format in logs

* Removed leftover comment

* Multiple changes after PR change requests.
Using an import flow for yaml config, removed unnecessary logs, added proper unload in __init__ and EnOceanDongle
Replaced config state machine by several flows.
Serial port validity check done in the EnOceanDongle class asynchronously, removed unique ID from config flow
Multiple cosmetic changes

* Multiple changes after PR change requests

* Added variable to store default value, as setdefault was caught returning None when the empty dict literal was passed as an argument

* Literal used directly

* Added tests for EnOcean config flows, changed static methods to bundle methods for bundle

* Updated variable name

* Added missing mock to test, replaced repeated magic strings by constants

* Changed imports to avoid an unused import warning from pylint on DOMAIN

* Adding pylint exception for unused import

* Added proper propagation of setup and unload to platforms, removed dead code, some syntax changes

* Removed setup_entry forwarding as the entities can only be configured using yaml

* Removed forwarding of unload

* Enabled code coverage for config flow only

* Clean up coveragerc

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-07-08 20:46:38 -04:00

111 lines
3.2 KiB
Python

"""Support for EnOcean light sources."""
import logging
import math
import voluptuous as vol
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
PLATFORM_SCHEMA,
SUPPORT_BRIGHTNESS,
LightEntity,
)
from homeassistant.const import CONF_ID, CONF_NAME
import homeassistant.helpers.config_validation as cv
from .device import EnOceanEntity
_LOGGER = logging.getLogger(__name__)
CONF_SENDER_ID = "sender_id"
DEFAULT_NAME = "EnOcean Light"
SUPPORT_ENOCEAN = SUPPORT_BRIGHTNESS
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_ID, default=[]): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Required(CONF_SENDER_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the EnOcean light platform."""
sender_id = config.get(CONF_SENDER_ID)
dev_name = config.get(CONF_NAME)
dev_id = config.get(CONF_ID)
add_entities([EnOceanLight(sender_id, dev_id, dev_name)])
class EnOceanLight(EnOceanEntity, LightEntity):
"""Representation of an EnOcean light source."""
def __init__(self, sender_id, dev_id, dev_name):
"""Initialize the EnOcean light source."""
super().__init__(dev_id, dev_name)
self._on_state = False
self._brightness = 50
self._sender_id = sender_id
@property
def name(self):
"""Return the name of the device if any."""
return self.dev_name
@property
def brightness(self):
"""Brightness of the light.
This method is optional. Removing it indicates to Home Assistant
that brightness is not supported for this light.
"""
return self._brightness
@property
def is_on(self):
"""If light is on."""
return self._on_state
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_ENOCEAN
def turn_on(self, **kwargs):
"""Turn the light source on or sets a specific dimmer value."""
brightness = kwargs.get(ATTR_BRIGHTNESS)
if brightness is not None:
self._brightness = brightness
bval = math.floor(self._brightness / 256.0 * 100.0)
if bval == 0:
bval = 1
command = [0xA5, 0x02, bval, 0x01, 0x09]
command.extend(self._sender_id)
command.extend([0x00])
self.send_command(command, [], 0x01)
self._on_state = True
def turn_off(self, **kwargs):
"""Turn the light source off."""
command = [0xA5, 0x02, 0x00, 0x01, 0x09]
command.extend(self._sender_id)
command.extend([0x00])
self.send_command(command, [], 0x01)
self._on_state = False
def value_changed(self, packet):
"""Update the internal state of this device.
Dimmer devices like Eltako FUD61 send telegram in different RORGs.
We only care about the 4BS (0xA5).
"""
if packet.data[0] == 0xA5 and packet.data[1] == 0x02:
val = packet.data[2]
self._brightness = math.floor(val / 100.0 * 256.0)
self._on_state = bool(val != 0)
self.schedule_update_ha_state()