1 """Support for the myStrom buttons."""
3 from __future__
import annotations
5 from http
import HTTPStatus
9 DOMAIN
as BINARY_SENSOR_DOMAIN,
17 _LOGGER = logging.getLogger(__name__)
23 async_add_entities: AddEntitiesCallback,
24 discovery_info: DiscoveryInfoType |
None =
None,
26 """Set up myStrom Binary Sensor."""
27 hass.http.register_view(
MyStromView(async_add_entities))
31 """View to handle requests from myStrom buttons."""
35 supported_actions = [
"single",
"double",
"long",
"touch"]
38 """Initialize the myStrom URL endpoint."""
42 async
def get(self, request):
43 """Handle the GET request received from a myStrom button."""
44 return await self.
_handle_handle(request.app[KEY_HASS], request.query)
47 """Handle requests to the myStrom endpoint."""
49 (parameter
for parameter
in data
if parameter
in self.
supported_actionssupported_actions),
53 if button_action
is None:
54 _LOGGER.error(
"Received unidentified message from myStrom button: %s", data)
56 f
"Received unidentified message: {data}",
57 HTTPStatus.UNPROCESSABLE_ENTITY,
60 button_id = data[button_action]
61 entity_id = f
"{BINARY_SENSOR_DOMAIN}.{button_id}_{button_action}"
62 if entity_id
not in self.
buttonsbuttons:
64 "New myStrom button/action detected: %s/%s", button_id, button_action
67 f
"{button_id}_{button_action}"
71 new_state = self.
buttonsbuttons[entity_id].state ==
"off"
72 self.
buttonsbuttons[entity_id].async_on_update(new_state)
77 """Representation of a myStrom button."""
79 _attr_should_poll =
False
82 """Initialize the myStrom Binary sensor."""
88 """Return the name of the sensor."""
93 """Return true if the binary sensor is on."""
98 """Receive an update."""
def __init__(self, button_id)
def async_on_update(self, value)
def _handle(self, hass, data)
def __init__(self, add_entities)
None async_write_ha_state(self)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)