Skip to content

Commit

Permalink
Add sensors platform to Watergate integration
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-the-hero committed Dec 12, 2024
1 parent 4ff41ed commit 0a0cd9e
Show file tree
Hide file tree
Showing 11 changed files with 1,329 additions and 17 deletions.
35 changes: 31 additions & 4 deletions homeassistant/components/watergate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@

_LOGGER = logging.getLogger(__name__)

WEBHOOK_ASO_REPORT_TYPE = "auto-shut-off-report"
WEBHOOK_TELEMETRY_TYPE = "telemetry"
WEBHOOK_VALVE_TYPE = "valve"
WEBHOOK_WIFI_CHANGED_TYPE = "wifi-changed"
WEBHOOK_POWER_SUPPLY_CHANGED_TYPE = "power-supply-changed"

PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.VALVE,
]

Expand Down Expand Up @@ -82,7 +88,6 @@ def get_webhook_handler(
async def async_webhook_handler(
hass: HomeAssistant, webhook_id: str, request: Request
) -> Response | None:
# Handle http post calls to the path.
if not request.body_exists:
return HomeAssistantView.json(
result="No Body", status_code=HTTPStatus.BAD_REQUEST
Expand All @@ -96,9 +101,31 @@ async def async_webhook_handler(

body_type = body.get("type")

coordinator_data = coordinator.data
if body_type == Platform.VALVE and coordinator_data:
coordinator_data.valve_state = data.state
if not (coordinator_data := coordinator.data):
pass

Check warning on line 105 in homeassistant/components/watergate/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/watergate/__init__.py#L105

Added line #L105 was not covered by tests
elif body_type == WEBHOOK_VALVE_TYPE:
coordinator_data.state.valve_state = data.state
elif body_type == WEBHOOK_TELEMETRY_TYPE:
errors = data.errors or {}
coordinator_data.telemetry.flow = (
data.flow if "flow" not in errors else None
)
coordinator_data.telemetry.pressure = (
data.pressure if "pressure" not in errors else None
)
coordinator_data.telemetry.water_temperature = (
data.temperature if "temperature" not in errors else None
)
elif body_type == WEBHOOK_WIFI_CHANGED_TYPE:
coordinator_data.networking.ip = data.ip
coordinator_data.networking.gateway = data.gateway
coordinator_data.networking.subnet = data.subnet
coordinator_data.networking.ssid = data.ssid
coordinator_data.networking.rssi = data.rssi
elif body_type == WEBHOOK_POWER_SUPPLY_CHANGED_TYPE:
coordinator_data.state.power_supply = data.supply
elif body_type == WEBHOOK_ASO_REPORT_TYPE:
coordinator_data.auto_shut_off_report = data

coordinator.async_set_updated_data(coordinator_data)

Expand Down
51 changes: 46 additions & 5 deletions homeassistant/components/watergate/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import logging

from watergate_local_api import WatergateApiException, WatergateLocalApiClient
from watergate_local_api.models import DeviceState
from watergate_local_api.models import (
AutoShutOffReport,
DeviceState,
NetworkingData,
TelemetryData,
)

from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand All @@ -14,7 +19,24 @@
_LOGGER = logging.getLogger(__name__)


class WatergateDataCoordinator(DataUpdateCoordinator[DeviceState]):
class WatergateAgregatedRequests:
"""Class to hold aggregated requests."""

def __init__(
self,
state: DeviceState,
telemetry: TelemetryData,
networking: NetworkingData,
auto_shut_off_report: AutoShutOffReport,
) -> None:
"""Initialize aggregated requests."""
self.state = state
self.telemetry = telemetry
self.networking = networking
self.auto_shut_off_report = auto_shut_off_report


class WatergateDataCoordinator(DataUpdateCoordinator[WatergateAgregatedRequests]):
"""Class to manage fetching watergate data."""

def __init__(self, hass: HomeAssistant, api: WatergateLocalApiClient) -> None:
Expand All @@ -27,9 +49,28 @@ def __init__(self, hass: HomeAssistant, api: WatergateLocalApiClient) -> None:
)
self.api = api

async def _async_update_data(self) -> DeviceState:
async def _async_update_data(self) -> WatergateAgregatedRequests:
try:
state = await self.api.async_get_device_state()
telemetry = await self.api.async_get_telemetry_data()
networking = await self.api.async_get_networking()
auto_shut_off_report = await self.api.async_get_auto_shut_off_report()
return WatergateAgregatedRequests(
state,
telemetry,
networking,
auto_shut_off_report,
)
except WatergateApiException as exc:
raise UpdateFailed from exc
return state
raise UpdateFailed(f"Sonic device is unavailable: {exc}") from exc

Check warning on line 65 in homeassistant/components/watergate/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/watergate/coordinator.py#L65

Added line #L65 was not covered by tests

def async_set_updated_data(self, data: WatergateAgregatedRequests) -> None:
"""Manually update data, notify listeners and DO NOT reset refresh interval."""

self.data = data
self.logger.debug(
"Manually updated %s data",
self.name,
)

self.async_update_listeners()
10 changes: 6 additions & 4 deletions homeassistant/components/watergate/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ def __init__(
"""Initialize the entity."""
super().__init__(coordinator)
self._api_client = coordinator.api
self._attr_unique_id = f"{coordinator.data.serial_number}.{entity_name}"
self._attr_unique_id = f"{coordinator.data.state.serial_number}.{entity_name}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.data.serial_number)},
identifiers={(DOMAIN, coordinator.data.state.serial_number)},
name="Sonic",
serial_number=coordinator.data.serial_number,
serial_number=coordinator.data.state.serial_number,
manufacturer=MANUFACTURER,
sw_version=coordinator.data.firmware_version if coordinator.data else None,
sw_version=coordinator.data.state.firmware_version
if coordinator.data
else None,
)
1 change: 1 addition & 0 deletions homeassistant/components/watergate/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ rules:
test-before-configure: done
test-before-setup: done
unique-config-entry: done

# Silver
config-entry-unloading: done
log-when-unavailable: todo
Expand Down
Loading

0 comments on commit 0a0cd9e

Please sign in to comment.