Move Hydrawise constants into const.py (#93357)

This commit is contained in:
David Knowles 2023-05-22 09:11:32 -04:00 committed by GitHub
parent ec12d9a197
commit 3d6d650a16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 39 deletions

View file

@ -1,6 +1,4 @@
"""Support for Hydrawise cloud.""" """Support for Hydrawise cloud."""
from datetime import timedelta
import logging
from hydrawiser.core import Hydrawiser from hydrawiser.core import Hydrawiser
from requests.exceptions import ConnectTimeout, HTTPError from requests.exceptions import ConnectTimeout, HTTPError
@ -15,22 +13,15 @@ from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.event import track_time_interval from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__) from .const import (
DATA_HYDRAWISE,
ALLOWED_WATERING_TIME = [5, 10, 15, 30, 45, 60] DOMAIN,
LOGGER,
CONF_WATERING_TIME = "watering_minutes" NOTIFICATION_ID,
NOTIFICATION_TITLE,
NOTIFICATION_ID = "hydrawise_notification" SCAN_INTERVAL,
NOTIFICATION_TITLE = "Hydrawise Setup" SIGNAL_UPDATE_HYDRAWISE,
)
DATA_HYDRAWISE = "hydrawise"
DOMAIN = "hydrawise"
DEFAULT_WATERING_TIME = 15
SCAN_INTERVAL = timedelta(seconds=120)
SIGNAL_UPDATE_HYDRAWISE = "hydrawise_update"
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
{ {
@ -55,7 +46,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
hydrawise = Hydrawiser(user_token=access_token) hydrawise = Hydrawiser(user_token=access_token)
hass.data[DATA_HYDRAWISE] = HydrawiseHub(hydrawise) hass.data[DATA_HYDRAWISE] = HydrawiseHub(hydrawise)
except (ConnectTimeout, HTTPError) as ex: except (ConnectTimeout, HTTPError) as ex:
_LOGGER.error("Unable to connect to Hydrawise cloud service: %s", str(ex)) LOGGER.error("Unable to connect to Hydrawise cloud service: %s", str(ex))
persistent_notification.create( persistent_notification.create(
hass, hass,
f"Error: {ex}<br />You will need to restart hass after fixing.", f"Error: {ex}<br />You will need to restart hass after fixing.",
@ -66,7 +57,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
def hub_refresh(event_time): def hub_refresh(event_time):
"""Call Hydrawise hub to refresh information.""" """Call Hydrawise hub to refresh information."""
_LOGGER.debug("Updating Hydrawise Hub component") LOGGER.debug("Updating Hydrawise Hub component")
hass.data[DATA_HYDRAWISE].data.update_controller_info() hass.data[DATA_HYDRAWISE].data.update_controller_info()
dispatcher_send(hass, SIGNAL_UPDATE_HYDRAWISE) dispatcher_send(hass, SIGNAL_UPDATE_HYDRAWISE)

View file

@ -1,8 +1,6 @@
"""Support for Hydrawise sprinkler binary sensors.""" """Support for Hydrawise sprinkler binary sensors."""
from __future__ import annotations from __future__ import annotations
import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -17,9 +15,8 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DATA_HYDRAWISE, HydrawiseEntity from . import HydrawiseEntity
from .const import DATA_HYDRAWISE, LOGGER
_LOGGER = logging.getLogger(__name__)
BINARY_SENSOR_STATUS = BinarySensorEntityDescription( BINARY_SENSOR_STATUS = BinarySensorEntityDescription(
key="status", key="status",
@ -82,7 +79,7 @@ class HydrawiseBinarySensor(HydrawiseEntity, BinarySensorEntity):
def update(self) -> None: def update(self) -> None:
"""Get the latest data and updates the state.""" """Get the latest data and updates the state."""
_LOGGER.debug("Updating Hydrawise binary sensor: %s", self.name) LOGGER.debug("Updating Hydrawise binary sensor: %s", self.name)
mydata = self.hass.data[DATA_HYDRAWISE].data mydata = self.hass.data[DATA_HYDRAWISE].data
if self.entity_description.key == "status": if self.entity_description.key == "status":
self._attr_is_on = mydata.status == "All good!" self._attr_is_on = mydata.status == "All good!"

View file

@ -0,0 +1,20 @@
"""Constants for the Hydrawise integration."""
from datetime import timedelta
import logging
LOGGER = logging.getLogger(__package__)
ALLOWED_WATERING_TIME = [5, 10, 15, 30, 45, 60]
CONF_WATERING_TIME = "watering_minutes"
NOTIFICATION_ID = "hydrawise_notification"
NOTIFICATION_TITLE = "Hydrawise Setup"
DATA_HYDRAWISE = "hydrawise"
DOMAIN = "hydrawise"
DEFAULT_WATERING_TIME = 15
SCAN_INTERVAL = timedelta(seconds=120)
SIGNAL_UPDATE_HYDRAWISE = "hydrawise_update"

View file

@ -1,8 +1,6 @@
"""Support for Hydrawise sprinkler sensors.""" """Support for Hydrawise sprinkler sensors."""
from __future__ import annotations from __future__ import annotations
import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -18,9 +16,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt from homeassistant.util import dt
from . import DATA_HYDRAWISE, HydrawiseEntity from . import HydrawiseEntity
from .const import DATA_HYDRAWISE, LOGGER
_LOGGER = logging.getLogger(__name__)
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
@ -76,7 +73,7 @@ class HydrawiseSensor(HydrawiseEntity, SensorEntity):
def update(self) -> None: def update(self) -> None:
"""Get the latest data and updates the states.""" """Get the latest data and updates the states."""
mydata = self.hass.data[DATA_HYDRAWISE].data mydata = self.hass.data[DATA_HYDRAWISE].data
_LOGGER.debug("Updating Hydrawise sensor: %s", self.name) LOGGER.debug("Updating Hydrawise sensor: %s", self.name)
relay_data = mydata.relays[self.data["relay"] - 1] relay_data = mydata.relays[self.data["relay"] - 1]
if self.entity_description.key == "watering_time": if self.entity_description.key == "watering_time":
if relay_data["timestr"] == "Now": if relay_data["timestr"] == "Now":
@ -85,7 +82,7 @@ class HydrawiseSensor(HydrawiseEntity, SensorEntity):
self._attr_native_value = 0 self._attr_native_value = 0
else: # _sensor_type == 'next_cycle' else: # _sensor_type == 'next_cycle'
next_cycle = min(relay_data["time"], TWO_YEAR_SECONDS) next_cycle = min(relay_data["time"], TWO_YEAR_SECONDS)
_LOGGER.debug("New cycle time: %s", next_cycle) LOGGER.debug("New cycle time: %s", next_cycle)
self._attr_native_value = dt.utc_from_timestamp( self._attr_native_value = dt.utc_from_timestamp(
dt.as_timestamp(dt.now()) + next_cycle dt.as_timestamp(dt.now()) + next_cycle
) )

View file

@ -1,7 +1,6 @@
"""Support for Hydrawise cloud switches.""" """Support for Hydrawise cloud switches."""
from __future__ import annotations from __future__ import annotations
import logging
from typing import Any from typing import Any
import voluptuous as vol import voluptuous as vol
@ -18,16 +17,15 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import ( from . import HydrawiseEntity
from .const import (
ALLOWED_WATERING_TIME, ALLOWED_WATERING_TIME,
CONF_WATERING_TIME, CONF_WATERING_TIME,
DATA_HYDRAWISE, DATA_HYDRAWISE,
DEFAULT_WATERING_TIME, DEFAULT_WATERING_TIME,
HydrawiseEntity, LOGGER,
) )
_LOGGER = logging.getLogger(__name__)
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = ( SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
SwitchEntityDescription( SwitchEntityDescription(
key="auto_watering", key="auto_watering",
@ -108,7 +106,7 @@ class HydrawiseSwitch(HydrawiseEntity, SwitchEntity):
"""Update device state.""" """Update device state."""
relay_data = self.data["relay"] - 1 relay_data = self.data["relay"] - 1
mydata = self.hass.data[DATA_HYDRAWISE].data mydata = self.hass.data[DATA_HYDRAWISE].data
_LOGGER.debug("Updating Hydrawise switch: %s", self.name) LOGGER.debug("Updating Hydrawise switch: %s", self.name)
if self.entity_description.key == "manual_watering": if self.entity_description.key == "manual_watering":
self._attr_is_on = mydata.relays[relay_data]["timestr"] == "Now" self._attr_is_on = mydata.relays[relay_data]["timestr"] == "Now"
elif self.entity_description.key == "auto_watering": elif self.entity_description.key == "auto_watering":