Add sensor platform to A. O. Smith integration (#105604)
* Add sensor platform to A. O. Smith integration * Fix typo * Remove unnecessary mixin * Simplify async_setup_entry
This commit is contained in:
parent
98b1bc9bed
commit
a595cd7141
6 changed files with 141 additions and 1 deletions
|
@ -13,7 +13,7 @@ from homeassistant.helpers import aiohttp_client
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import AOSmithCoordinator
|
from .coordinator import AOSmithCoordinator
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.WATER_HEATER]
|
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.WATER_HEATER]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|
|
@ -14,3 +14,9 @@ REGULAR_INTERVAL = timedelta(seconds=30)
|
||||||
|
|
||||||
# Update interval to be used while a mode or setpoint change is in progress.
|
# Update interval to be used while a mode or setpoint change is in progress.
|
||||||
FAST_INTERVAL = timedelta(seconds=1)
|
FAST_INTERVAL = timedelta(seconds=1)
|
||||||
|
|
||||||
|
HOT_WATER_STATUS_MAP = {
|
||||||
|
"LOW": "low",
|
||||||
|
"MEDIUM": "medium",
|
||||||
|
"HIGH": "high",
|
||||||
|
}
|
||||||
|
|
75
homeassistant/components/aosmith/sensor.py
Normal file
75
homeassistant/components/aosmith/sensor.py
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
"""The sensor platform for the A. O. Smith integration."""
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import (
|
||||||
|
SensorDeviceClass,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import AOSmithData
|
||||||
|
from .const import DOMAIN, HOT_WATER_STATUS_MAP
|
||||||
|
from .coordinator import AOSmithCoordinator
|
||||||
|
from .entity import AOSmithEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(kw_only=True)
|
||||||
|
class AOSmithSensorEntityDescription(SensorEntityDescription):
|
||||||
|
"""Define sensor entity description class."""
|
||||||
|
|
||||||
|
value_fn: Callable[[dict[str, Any]], str | int | None]
|
||||||
|
|
||||||
|
|
||||||
|
ENTITY_DESCRIPTIONS: tuple[AOSmithSensorEntityDescription, ...] = (
|
||||||
|
AOSmithSensorEntityDescription(
|
||||||
|
key="hot_water_availability",
|
||||||
|
translation_key="hot_water_availability",
|
||||||
|
icon="mdi:water-thermometer",
|
||||||
|
device_class=SensorDeviceClass.ENUM,
|
||||||
|
options=["low", "medium", "high"],
|
||||||
|
value_fn=lambda device: HOT_WATER_STATUS_MAP.get(
|
||||||
|
device.get("data", {}).get("hotWaterStatus")
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up A. O. Smith sensor platform."""
|
||||||
|
data: AOSmithData = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
AOSmithSensorEntity(data.coordinator, description, junction_id)
|
||||||
|
for description in ENTITY_DESCRIPTIONS
|
||||||
|
for junction_id in data.coordinator.data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AOSmithSensorEntity(AOSmithEntity, SensorEntity):
|
||||||
|
"""The sensor entity for the A. O. Smith integration."""
|
||||||
|
|
||||||
|
entity_description: AOSmithSensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: AOSmithCoordinator,
|
||||||
|
description: AOSmithSensorEntityDescription,
|
||||||
|
junction_id: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the entity."""
|
||||||
|
super().__init__(coordinator, junction_id)
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_unique_id = f"{description.key}_{junction_id}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> str | int | None:
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
return self.entity_description.value_fn(self.device)
|
|
@ -24,5 +24,17 @@
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"entity": {
|
||||||
|
"sensor": {
|
||||||
|
"hot_water_availability": {
|
||||||
|
"name": "Hot water availability",
|
||||||
|
"state": {
|
||||||
|
"low": "Low",
|
||||||
|
"medium": "Medium",
|
||||||
|
"high": "High"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
tests/components/aosmith/snapshots/test_sensor.ambr
Normal file
20
tests/components/aosmith/snapshots/test_sensor.ambr
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# serializer version: 1
|
||||||
|
# name: test_state
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'enum',
|
||||||
|
'friendly_name': 'My water heater Hot water availability',
|
||||||
|
'icon': 'mdi:water-thermometer',
|
||||||
|
'options': list([
|
||||||
|
'low',
|
||||||
|
'medium',
|
||||||
|
'high',
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.my_water_heater_hot_water_availability',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'low',
|
||||||
|
})
|
||||||
|
# ---
|
27
tests/components/aosmith/test_sensor.py
Normal file
27
tests/components/aosmith/test_sensor.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
"""Tests for the sensor platform of the A. O. Smith integration."""
|
||||||
|
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
async def test_setup(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
init_integration: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test the setup of the sensor entity."""
|
||||||
|
entry = entity_registry.async_get("sensor.my_water_heater_hot_water_availability")
|
||||||
|
assert entry
|
||||||
|
assert entry.unique_id == "hot_water_availability_junctionId"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_state(
|
||||||
|
hass: HomeAssistant, init_integration: MockConfigEntry, snapshot: SnapshotAssertion
|
||||||
|
) -> None:
|
||||||
|
"""Test the state of the sensor entity."""
|
||||||
|
state = hass.states.get("sensor.my_water_heater_hot_water_availability")
|
||||||
|
assert state == snapshot
|
Loading…
Add table
Add a link
Reference in a new issue