Skip to content

Commit

Permalink
Added binary_sensor for update available (Zappi), sensors for firmwar…
Browse files Browse the repository at this point in the history
…e version and serial number (all devices), fixes #237
  • Loading branch information
CJNE committed Mar 26, 2023
1 parent b0de5a1 commit 8462d6c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
61 changes: 61 additions & 0 deletions custom_components/myenergi/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Sensor platform for myenergi."""
import operator

from homeassistant.components.binary_sensor import BinarySensorEntity
from pymyenergi import ZAPPI

from .const import DOMAIN
from .entity import MyenergiEntity


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup binary sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
sensors = []
# Don't cause a refresh when fetching sensors
all_devices = await coordinator.client.get_devices("all", False)
for device in all_devices:
# Sensors available in all devices
if device.kind in [ZAPPI]:
sensors.append(
MyenergiBinarySensor(
coordinator,
device,
entry,
{
"name": "Update available",
"prop_name": "update_available",
"icon": None,
"attrs": {},
},
)
)
async_add_devices(sensors)


class MyenergiBinarySensor(MyenergiEntity, BinarySensorEntity):
"""myenergi Binary Sensor class."""

def __init__(self, coordinator, device, config_entry, meta):
super().__init__(coordinator, device, config_entry, meta)

@property
def name(self):
"""Return the name of the sensor."""
return f"myenergi {self.device.name} {self.meta['name']}"

@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return f"{self.config_entry.entry_id}-{self.device.serial_number}-{self.meta['prop_name']}"

@property
def is_on(self):
"""Return the state of the sensor."""
value = operator.attrgetter(self.meta["prop_name"])(self.device)
return value

@property
def icon(self):
"""Return the icon of the sensor."""
return self.meta["icon"]
8 changes: 3 additions & 5 deletions custom_components/myenergi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
NAME = "myenergi"
DOMAIN = "myenergi"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.18"
VERSION = "0.0.19"

ATTRIBUTION = "Data provided by myenergi"
ISSUE_URL = "https://github.com/cjne/myenergi/issues"

# Icons
ICON = "mdi:format-quote-close"

# Device classes
BINARY_SENSOR_DEVICE_CLASS = "connectivity"

# Platforms
SENSOR = "sensor"
BINARY_SENSOR = "binary_sensor"
SELECT = "select"
NUMBER = "number"
PLATFORMS = [SENSOR, SELECT, NUMBER]
PLATFORMS = [SENSOR, BINARY_SENSOR, SELECT, NUMBER]


# Configuration and options
Expand Down
31 changes: 31 additions & 0 deletions custom_components/myenergi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ async def async_setup_entry(hass, entry, async_add_devices):
sensors = []
# Don't cause a refresh when fetching sensors
all_devices = await coordinator.client.get_devices("all", False)
sensors.append(
MyenergiHubSensor(
coordinator,
entry,
create_meta("Hub firmware", "firmware_version", icon="mdi:numeric"),
)
)
sensors.append(
MyenergiHubSensor(
coordinator,
entry,
create_meta("Hub serial number", "serial_number", icon="mdi:numeric"),
)
)
sensors.append(
MyenergiHubSensor(
coordinator,
Expand Down Expand Up @@ -236,6 +250,23 @@ async def async_setup_entry(hass, entry, async_add_devices):
)
for device in all_devices:
# Sensors available in all devices

sensors.append(
MyenergiSensor(
coordinator,
device,
entry,
create_meta("Firmware", "firmware_version", icon="mdi:numeric"),
)
)
sensors.append(
MyenergiSensor(
coordinator,
device,
entry,
create_meta("Serial number", "serial_number", icon="mdi:numeric"),
)
)
sensors.append(
MyenergiSensor(
coordinator,
Expand Down

0 comments on commit 8462d6c

Please sign in to comment.