Added rain_start and lightningstrike event to publish on the event bus (#146652)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>pull/152433/head
parent
0254285285
commit
892f3f267b
|
@ -17,6 +17,7 @@ from homeassistant.helpers.start import async_at_started
|
|||
from .const import DOMAIN, LOGGER, format_dispatch_call
|
||||
|
||||
PLATFORMS = [
|
||||
Platform.EVENT,
|
||||
Platform.SENSOR,
|
||||
]
|
||||
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
"""Event entities for the WeatherFlow integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from pyweatherflowudp.device import EVENT_RAIN_START, EVENT_STRIKE, WeatherFlowDevice
|
||||
|
||||
from homeassistant.components.event import EventEntity, EventEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN, LOGGER, format_dispatch_call
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class WeatherFlowEventEntityDescription(EventEntityDescription):
|
||||
"""Describes a WeatherFlow event entity."""
|
||||
|
||||
wf_event: str
|
||||
event_types: list[str]
|
||||
|
||||
|
||||
EVENT_DESCRIPTIONS: list[WeatherFlowEventEntityDescription] = [
|
||||
WeatherFlowEventEntityDescription(
|
||||
key="precip_start_event",
|
||||
translation_key="precip_start_event",
|
||||
event_types=["precipitation_start"],
|
||||
wf_event=EVENT_RAIN_START,
|
||||
),
|
||||
WeatherFlowEventEntityDescription(
|
||||
key="lightning_strike_event",
|
||||
translation_key="lightning_strike_event",
|
||||
event_types=["lightning_strike"],
|
||||
wf_event=EVENT_STRIKE,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up WeatherFlow event entities using config entry."""
|
||||
|
||||
@callback
|
||||
def async_add_events(device: WeatherFlowDevice) -> None:
|
||||
LOGGER.debug("Adding events for %s", device)
|
||||
async_add_entities(
|
||||
WeatherFlowEventEntity(device, description)
|
||||
for description in EVENT_DESCRIPTIONS
|
||||
)
|
||||
|
||||
config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
format_dispatch_call(config_entry),
|
||||
async_add_events,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class WeatherFlowEventEntity(EventEntity):
|
||||
"""Generic WeatherFlow event entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
entity_description: WeatherFlowEventEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
device: WeatherFlowDevice,
|
||||
description: WeatherFlowEventEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the WeatherFlow event entity."""
|
||||
|
||||
self.device = device
|
||||
self.entity_description = description
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device.serial_number)},
|
||||
manufacturer="WeatherFlow",
|
||||
model=device.model,
|
||||
name=device.serial_number,
|
||||
sw_version=device.firmware_revision,
|
||||
)
|
||||
self._attr_unique_id = f"{device.serial_number}_{description.key}"
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to the configured WeatherFlow device event."""
|
||||
self.async_on_remove(
|
||||
self.device.on(self.entity_description.wf_event, self._handle_event)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_event(self, event) -> None:
|
||||
self._trigger_event(
|
||||
self.entity_description.event_types[0],
|
||||
{},
|
||||
)
|
||||
self.async_write_ha_state()
|
|
@ -38,6 +38,14 @@
|
|||
"337.5": "mdi:arrow-up"
|
||||
}
|
||||
}
|
||||
},
|
||||
"event": {
|
||||
"lightning_strike_event": {
|
||||
"default": "mdi:weather-lightning"
|
||||
},
|
||||
"precip_start_event": {
|
||||
"default": "mdi:weather-rainy"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,6 +79,14 @@
|
|||
"wind_lull": {
|
||||
"name": "Wind lull"
|
||||
}
|
||||
},
|
||||
"event": {
|
||||
"lightning_strike_event": {
|
||||
"name": "Lightning strike"
|
||||
},
|
||||
"precip_start_event": {
|
||||
"name": "Precipitation start"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue