core/homeassistant/components/enocean/binary_sensor.py

123 lines
3.6 KiB
Python
Raw Normal View History

"""Support for EnOcean binary sensors."""
from __future__ import annotations
from enocean.utils import combine_hex
2016-08-25 04:35:09 +00:00
import voluptuous as vol
from homeassistant.components.binary_sensor import (
2019-07-31 19:25:30 +00:00
DEVICE_CLASSES_SCHEMA,
PLATFORM_SCHEMA,
BinarySensorEntity,
2019-07-31 19:25:30 +00:00
)
from homeassistant.const import CONF_DEVICE_CLASS, CONF_ID, CONF_NAME
from homeassistant.core import HomeAssistant
2016-08-25 04:35:09 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2016-08-25 04:35:09 +00:00
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-09 00:46:38 +00:00
from .device import EnOceanEntity
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "EnOcean binary sensor"
DEPENDENCIES = ["enocean"]
EVENT_BUTTON_PRESSED = "button_pressed"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
}
)
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Binary Sensor platform for EnOcean."""
2016-08-25 04:35:09 +00:00
dev_id = config.get(CONF_ID)
dev_name = config.get(CONF_NAME)
device_class = config.get(CONF_DEVICE_CLASS)
2016-08-25 04:35:09 +00:00
add_entities([EnOceanBinarySensor(dev_id, dev_name, device_class)])
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-09 00:46:38 +00:00
class EnOceanBinarySensor(EnOceanEntity, BinarySensorEntity):
"""Representation of EnOcean binary sensors such as wall switches.
Supported EEPs (EnOcean Equipment Profiles):
- F6-02-01 (Light and Blind Control - Application Style 2)
- F6-02-02 (Light and Blind Control - Application Style 1)
"""
def __init__(self, dev_id, dev_name, device_class):
"""Initialize the EnOcean binary sensor."""
super().__init__(dev_id, dev_name)
self._device_class = device_class
self.which = -1
self.onoff = -1
self._attr_unique_id = f"{combine_hex(dev_id)}-{device_class}"
@property
def name(self):
"""Return the default name for the binary sensor."""
return self.dev_name
2016-08-25 04:35:09 +00:00
@property
def device_class(self):
2016-08-25 04:35:09 +00:00
"""Return the class of this sensor."""
return self._device_class
2016-08-25 04:35:09 +00:00
def value_changed(self, packet):
"""Fire an event with the data that have changed.
This method is called when there is an incoming packet associated
with this platform.
Example packet data:
- 2nd button pressed
['0xf6', '0x10', '0x00', '0x2d', '0xcf', '0x45', '0x30']
- button released
['0xf6', '0x00', '0x00', '0x2d', '0xcf', '0x45', '0x20']
"""
# Energy Bow
pushed = None
if packet.data[6] == 0x30:
pushed = 1
elif packet.data[6] == 0x20:
pushed = 0
self.schedule_update_ha_state()
action = packet.data[1]
if action == 0x70:
self.which = 0
self.onoff = 0
elif action == 0x50:
self.which = 0
self.onoff = 1
elif action == 0x30:
self.which = 1
self.onoff = 0
elif action == 0x10:
self.which = 1
self.onoff = 1
elif action == 0x37:
self.which = 10
self.onoff = 0
elif action == 0x15:
self.which = 10
self.onoff = 1
2019-07-31 19:25:30 +00:00
self.hass.bus.fire(
EVENT_BUTTON_PRESSED,
{
"id": self.dev_id,
"pushed": pushed,
"which": self.which,
"onoff": self.onoff,
},
)