Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add power sensor for MyStrom #69745

Closed
wants to merge 12 commits into from
72 changes: 70 additions & 2 deletions homeassistant/components/mystrom/switch.py
@@ -1,11 +1,18 @@
"""Support for myStrom switches/plugs."""
from __future__ import annotations
from contextlib import suppress

import logging
from typing import Any

from pymystrom.exceptions import MyStromConnectionError
from pymystrom.switch import MyStromSwitch as PyMyStromSwitch
import voluptuous as vol
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
)

from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
Expand All @@ -16,6 +23,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import convert

from .const import DOMAIN, MANUFACTURER

Expand All @@ -36,7 +44,13 @@ async def async_setup_entry(
) -> None:
"""Set up the myStrom entities."""
device = hass.data[DOMAIN][entry.entry_id].device
async_add_entities([MyStromSwitch(device, entry.title)])
async_add_entities(
[
MyStromSwitch(device, entry.title),
MyStromPowerSensor(device, entry.title),
MyStromTemperatureSensor(device, entry.title),
]
)


async def async_setup_platform(
Expand Down Expand Up @@ -73,7 +87,7 @@ class MyStromSwitch(SwitchEntity):
_attr_has_entity_name = True
_attr_name = None

def __init__(self, plug, name):
def __init__(self, plug: PyMyStromSwitch, name):
"""Initialize the myStrom switch/plug."""
self.plug = plug
self._attr_unique_id = self.plug.mac
Expand Down Expand Up @@ -108,3 +122,57 @@ async def async_update(self) -> None:
if self.available:
self._attr_available = False
_LOGGER.error("No route to myStrom plug")


class MyStromPowerSensor(SensorEntity):
OneCyrus marked this conversation as resolved.
Show resolved Hide resolved
"""Representation of a MySwitch Power Sensor."""

_attr_device_class = SensorDeviceClass.POWER
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = SensorDeviceClass.POWER

def __init__(self, plug: PyMyStromSwitch, name) -> None:
"""Initialize the sensor."""
self._plug = plug
self._attr_name = f"{name} Power"
self._attr_unique_id = f"{plug.mac}.power"
# self.entity_id = f"sensor.{plug.mac}.power"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._plug.mac)},
name=name,
manufacturer=MANUFACTURER,
sw_version=self._plug.firmware,
)

async def async_update(self):
"""Update the state."""
with suppress(KeyError, ValueError):
await self._plug.get_state()
self._attr_native_value = convert(self._plug.consumption, float)


class MyStromTemperatureSensor(SensorEntity):
"""Representation of a MySwitch Temperature Sensor."""

_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = SensorDeviceClass.TEMPERATURE

def __init__(self, plug: PyMyStromSwitch, name) -> None:
"""Initialize the sensor."""
self._plug = plug
self._attr_name = f"{name} Temperature"
self._attr_unique_id = f"{plug.mac}.temperature"
# self.entity_id = f"sensor.{plug.mac}.temperature"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._plug.mac)},
name=name,
manufacturer=MANUFACTURER,
sw_version=self._plug.firmware,
)

async def async_update(self):
"""Update the state."""
with suppress(KeyError, ValueError):
await self._plug.get_state()
self._attr_native_value = convert(self._plug.temperature, float)