-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sensors, throttle updates to 5s
- Loading branch information
Showing
7 changed files
with
224 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from homeassistant.components import mqtt | ||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorEntity, | ||
BinarySensorDeviceClass, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_PREFIX, EntityCategory | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from .entity import OpenMowerMqttEntity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
# Make sure MQTT integration is enabled and the client is available | ||
if not await mqtt.async_wait_for_mqtt_client(hass): | ||
_LOGGER.error("MQTT integration is not available") | ||
return | ||
|
||
prefix = entry.data[CONF_PREFIX] | ||
async_add_entities( | ||
[ | ||
OpenMowerMqttBinarySensorEntity( | ||
"Emergency", prefix, "robot_state/json", "emergency" | ||
), | ||
OpenMowerMqttBinarySensorEntity( | ||
"Is charging", prefix, "robot_state/json", "is_charging" | ||
), | ||
] | ||
) | ||
|
||
|
||
class OpenMowerMqttBinarySensorEntity(OpenMowerMqttEntity, BinarySensorEntity): | ||
def _process_update(self, value): | ||
self._attr_is_on = bool(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import timedelta | ||
from typing import Optional | ||
|
||
from homeassistant.components import mqtt | ||
from homeassistant.components.openmower import DOMAIN | ||
from homeassistant.core import callback | ||
from homeassistant.helpers.device_registry import DeviceInfo | ||
from homeassistant.helpers.entity import Entity | ||
from homeassistant.util import slugify, Throttle | ||
from homeassistant.util.json import json_loads_object | ||
|
||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5) | ||
|
||
|
||
class OpenMowerMqttEntity(Entity): | ||
_attr_has_entity_name = True | ||
|
||
def __init__(self, name: str, prefix: str, topic: str, key: Optional[str]) -> None: | ||
self._attr_name = name | ||
self._attr_unique_id = slugify(f"{prefix}_{name}").lower() | ||
|
||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, slugify(prefix))}, | ||
manufacturer="OpenMower", | ||
name=slugify(prefix).capitalize(), | ||
) | ||
|
||
self._mqtt_topic = topic | ||
self._mqtt_topic_prefix = prefix | ||
self._mqtt_payload_json_key = key | ||
|
||
if self._mqtt_topic_prefix and self._mqtt_topic_prefix[-1] != "/": | ||
self._mqtt_topic_prefix = self._mqtt_topic_prefix + "/" | ||
|
||
async def async_added_to_hass(self) -> None: | ||
await mqtt.async_subscribe( | ||
self.hass, | ||
self._mqtt_topic_prefix + self._mqtt_topic, | ||
self._async_robot_state_received, | ||
0, | ||
) | ||
|
||
@callback | ||
def _async_robot_state_received(self, msg: mqtt.ReceiveMessage) -> None: | ||
self._update_state(msg) | ||
|
||
@Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
def _update_state(self, msg): | ||
if self._mqtt_payload_json_key: | ||
value_json = json_loads_object(msg.payload) | ||
self._process_update(value_json[self._mqtt_payload_json_key]) | ||
else: | ||
self._process_update(msg.payload) | ||
self.async_write_ha_state() | ||
|
||
def _process_update(self, value): | ||
raise NotImplementedError("Subclasses should implement this!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters