-
-
Notifications
You must be signed in to change notification settings - Fork 28.6k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sensors for myStrom plugs #97024
Merged
joostlek
merged 17 commits into
home-assistant:dev
from
MadMonkey87:myStrom-improvements
Oct 18, 2023
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
92c1724
support sensors for myStrom plugs
MadMonkey87 5ce5e6a
added myStrom sensor to coveragerc
MadMonkey87 16c039f
some improvements from pr reviews
MadMonkey87 8150f58
Merge branch 'dev' into myStrom-improvements
MadMonkey87 0147a5a
adapt to the SensorEntityDescription pattern
MadMonkey87 fe7be6d
Merge branch 'dev' into myStrom-improvements
MadMonkey87 9575fee
Apply suggestions from code review
MadMonkey87 8a6899e
Merge branch 'dev' into myStrom-improvements
MadMonkey87 d1b3d02
Update homeassistant/components/mystrom/sensor.py
MadMonkey87 ab9d1d3
Update homeassistant/components/mystrom/sensor.py
MadMonkey87 1299e10
Merge branch 'home-assistant:dev' into myStrom-improvements
MadMonkey87 8f1890c
Merge branch 'dev' into myStrom-improvements
MadMonkey87 32c10b9
Update __init__.py
joostlek c289e4a
Update const.py
joostlek d62b8fa
Update sensor.py
joostlek 69f618c
Apply suggestions from code review
joostlek aaf9741
Merge branch 'dev' into myStrom-improvements
joostlek File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,92 @@ | ||
"""Support for myStrom sensors of switches/plugs.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
|
||
from pymystrom.switch import MyStromSwitch | ||
|
||
from homeassistant.components.sensor import ( | ||
SensorDeviceClass, | ||
SensorEntity, | ||
SensorEntityDescription, | ||
SensorStateClass, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import UnitOfPower, UnitOfTemperature | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.device_registry import DeviceInfo | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import DOMAIN, MANUFACTURER | ||
|
||
|
||
@dataclass | ||
class MyStromSwitchSensorEntityDescription(SensorEntityDescription): | ||
"""Class describing mystrom switch sensor entities.""" | ||
|
||
value_fn: Callable[[MyStromSwitch], float | None] = lambda _: None | ||
|
||
|
||
SENSOR_TYPES: tuple[MyStromSwitchSensorEntityDescription, ...] = ( | ||
MyStromSwitchSensorEntityDescription( | ||
key="consumption", | ||
device_class=SensorDeviceClass.POWER, | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=UnitOfPower.WATT, | ||
value_fn=lambda device: device.consumption, | ||
), | ||
MyStromSwitchSensorEntityDescription( | ||
key="temperature", | ||
device_class=SensorDeviceClass.TEMPERATURE, | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=UnitOfTemperature.CELSIUS, | ||
value_fn=lambda device: device.temperature, | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up the myStrom entities.""" | ||
device: MyStromSwitch = hass.data[DOMAIN][entry.entry_id].device | ||
sensors = [] | ||
|
||
for description in SENSOR_TYPES: | ||
if description.value_fn(device) is not None: | ||
sensors.append(MyStromSwitchSensor(device, entry.title, description)) | ||
|
||
async_add_entities(sensors) | ||
|
||
|
||
class MyStromSwitchSensor(SensorEntity): | ||
"""Representation of the consumption or temperature of a myStrom switch/plug.""" | ||
|
||
entity_description: MyStromSwitchSensorEntityDescription | ||
device: MyStromSwitch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need a type annotation and class attribute for |
||
|
||
_attr_has_entity_name = True | ||
|
||
def __init__( | ||
self, | ||
device: MyStromSwitch, | ||
name: str, | ||
description: MyStromSwitchSensorEntityDescription, | ||
) -> None: | ||
"""Initialize the sensor.""" | ||
self.device = device | ||
self.entity_description = description | ||
|
||
self._attr_unique_id = f"{device.mac}-{description.key}" | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, device.mac)}, | ||
name=name, | ||
manufacturer=MANUFACTURER, | ||
sw_version=device.firmware, | ||
) | ||
|
||
@property | ||
def native_value(self) -> float | None: | ||
"""Return the value of the sensor.""" | ||
return self.entity_description.value_fn(self.device) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please sort 🔡.