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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Detect sensor plugged with 1wire #26

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/aquarea/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
Expand Down
74 changes: 73 additions & 1 deletion custom_components/aquarea/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import logging

from homeassistant.components import mqtt
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorEntityDescription,
)
from homeassistant.components.template.sensor import SensorTemplate
from homeassistant.helpers import template as template_helper
from homeassistant.const import (
Expand Down Expand Up @@ -38,7 +42,16 @@ async def async_setup_entry(
HeishaMonSensor(hass, description, config_entry) for description in SENSORS
]
all_sensors = real_sensors + build_virtual_sensors(hass, config_entry, real_sensors)
dallas_list_config = SensorEntityDescription(
key="panasonic_heat_pump/1wire/+",
name="HeishaMon 1wire sensors",
)
async_add_entities(all_sensors)
# this special sensor will listen to 1wire topics and create new sensors accordingly
dallas_listing = DallasListSensor(
hass, dallas_list_config, config_entry, async_add_entities
)
async_add_entities([dallas_listing])


def build_virtual_sensors(
Expand Down Expand Up @@ -168,6 +181,65 @@ def device_info(self):
return build_device_info(DeviceType.HEATPUMP)


class DallasListSensor(SensorEntity):
def __init__(
self,
hass: HomeAssistant,
description: SensorEntityDescription,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
self.hass = hass
self.entity_description = description
self.config_entry = config_entry
self.config_entry_entry_id = config_entry.entry_id

slug = slugify(description.key.replace("/", "_"))
self.entity_id = f"sensor.{slug}"
self._attr_unique_id = (
f"{config_entry.entry_id}-dallas-listing" # ⚠ we can't have two of this
)
self.async_add_entities = async_add_entities
self._known_1wire = []

async def async_added_to_hass(self) -> None:
"""Subscribe to MQTT events"""
await super().async_added_to_hass()

@callback
def message_received(message):
device_id = message.topic.split("/")[-1]
if device_id not in self._known_1wire:
description = HeishaMonSensorEntityDescription(
heishamon_topic_id=f"1wire-{device_id}",
key=message.topic,
name=f"HeishaMon 1wire {device_id}",
native_unit_of_measurement="°C", # we assume everything will be temperature
device_class=SensorDeviceClass.TEMPERATURE,
device=DeviceType.HEISHAMON,
)
sensor = HeishaMonSensor(self.hass, description, self.config_entry)
_LOGGER.info(
f"Detected new 1wire sensor with id {device_id}, creating a new sensor"
)
sensor._attr_native_value = float(
message.payload
) # set immediately a known state
self.async_add_entities([sensor])
self._known_1wire.append(device_id)
self._known_1wire.sort()
self._attr_native_value = ", ".join(self._known_1wire)
self.async_write_ha_state()

await mqtt.async_subscribe(
self.hass, self.entity_description.key, message_received, 1
)

@property
def device_info(self):
return build_device_info(DeviceType.HEISHAMON)


class HeishaMonSensor(SensorEntity):
"""Representation of a HeishaMon sensor that is updated via MQTT."""

Expand Down