Skip to content

Commit

Permalink
Refresh temperature sensors when units are updated
Browse files Browse the repository at this point in the history
Revert change to input validator in config_flow
  • Loading branch information
jason0x43 committed Feb 25, 2020
1 parent effca5c commit 6eaf05d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 30 deletions.
1 change: 1 addition & 0 deletions custom_components/hubitat/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ async def async_setup_entry(
if attr[0] in devices[i].attributes
]
async_add_entities(sensors)
hub.add_entities(sensors)
_LOGGER.debug(f"Added entities for binary sensors: {sensors}")


Expand Down
1 change: 1 addition & 0 deletions custom_components/hubitat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,4 @@ async def async_setup_entry(
if is_thermostat(devices[i])
]
async_add_entities(therms)
hub.add_entities(therms)
33 changes: 17 additions & 16 deletions custom_components/hubitat/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@
)


async def validate_input(data: Dict[str, Any]) -> Dict[str, Any]:
"""Validate that the user input allows us to connect."""

# data has the keys from CONFIG_SCHEMA with values provided by the user.
host: str = data[CONF_HOST]
app_id: str = data[CONF_APP_ID]
token: str = data[CONF_ACCESS_TOKEN]

hub = HubitatHub(host, app_id, token)
await hub.check_config()

return {
"label": f"Hubitat ({hub.mac})",
}


class HubitatConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Hubitat."""

Expand All @@ -63,7 +79,7 @@ async def async_step_user(

if user_input is not None:
try:
info = await self.async_validate_input(user_input)
info = await validate_input(user_input)
entry_data = deepcopy(user_input)

placeholders: Dict[str, str] = {}
Expand Down Expand Up @@ -104,21 +120,6 @@ async def async_step_user(
step_id="user", data_schema=CONFIG_SCHEMA, errors=form_errors,
)

async def async_validate_input(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Validate that the user input allows us to connect."""

# data has the keys from CONFIG_SCHEMA with values provided by the user.
host: str = data[CONF_HOST]
app_id: str = data[CONF_APP_ID]
token: str = data[CONF_ACCESS_TOKEN]

hub = HubitatHub(host, app_id, token)
await hub.check_config()

return {
"label": f"Hubitat ({hub.mac})",
}


class HubitatOptionsFlow(OptionsFlow):
"""Handle an options flow for Hubitat."""
Expand Down
1 change: 1 addition & 0 deletions custom_components/hubitat/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,5 @@ async def async_setup_entry(
if cap in devices[i].capabilities
]
async_add_entities(covers)
hub.add_entities(covers)
_LOGGER.debug(f"Added entities for covers: {covers}")
49 changes: 36 additions & 13 deletions custom_components/hubitat/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC, abstractmethod
from json import loads
from logging import getLogger
from typing import Any, Callable, Dict, List, Mapping, Optional, Union, cast
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Union, cast

from hubitatmaker import (
CAP_COLOR_CONTROL,
Expand All @@ -23,6 +23,7 @@
Hub as HubitatHub,
)

from homeassistant.components.sensor import DEVICE_CLASS_TEMPERATURE
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_HIDDEN,
Expand Down Expand Up @@ -66,9 +67,16 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry, index: int):

self.hass = hass
self.config_entry = entry
self.entity_ids: List[int] = []
self.entities: List["HubitatEntity"] = []
self.event_emitters: List["HubitatEventEmitter"] = []

self._temperature_unit = (
entry.options.get(
CONF_TEMPERATURE_UNIT, entry.data.get(CONF_TEMPERATURE_UNIT)
)
or TEMP_F
)

if index == 1:
self._hub_entity_id = "hubitat.hub"
else:
Expand Down Expand Up @@ -106,23 +114,25 @@ def token(self) -> str:

@property
def temperature_unit(self) -> str:
entry = self.config_entry
return (
entry.options.get(
CONF_TEMPERATURE_UNIT, entry.data.get(CONF_TEMPERATURE_UNIT)
)
or TEMP_F
)
return self._temperature_unit

def add_device_listener(self, device_id: str, listener: Listener):
return self._hub.add_device_listener(device_id, listener)

def add_event_emitter(self, emitter: "HubitatEventEmitter") -> None:
self.event_emitters.append(emitter)
def add_entities(self, entities: Sequence["HubitatEntity"]) -> None:
self.entities.extend(entities)

def add_event_emitters(self, emitters: Sequence["HubitatEventEmitter"]) -> None:
self.event_emitters.extend(emitters)

def remove_device_listeners(self, device_id: str) -> None:
self._hub.remove_device_listeners(device_id)

def set_temperature_unit(self, temp_unit: str) -> None:
"""Set the hub's temperature units."""
_LOGGER.debug("Setting hub temperature unit to %s", temp_unit)
self._temperature_unit = temp_unit

def stop(self) -> None:
self._hub.stop()

Expand Down Expand Up @@ -180,13 +190,25 @@ async def async_update_device_registry(self) -> None:
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
hub = get_hub(hass, entry.entry_id)

port = (
entry.options.get(CONF_SERVER_PORT, entry.data.get(CONF_SERVER_PORT)) or 0
)
if port != hub.port:
_LOGGER.debug("Setting event listener port to %s", port)
await hub.set_port(port)

temp_unit = (
entry.options.get(
CONF_TEMPERATURE_UNIT, entry.data.get(CONF_TEMPERATURE_UNIT)
)
or TEMP_F
)
if temp_unit != hub.temperature_unit:
hub.set_temperature_unit(temp_unit)
for entity in hub.entities:
if entity.device_class == DEVICE_CLASS_TEMPERATURE:
entity.async_schedule_update_ha_state()

hass.states.async_set(
hub.entity_id, "connected", {CONF_TEMPERATURE_UNIT: hub.temperature_unit}
)
Expand All @@ -206,7 +228,8 @@ async def send_command(

async def set_port(self, port: int) -> None:
"""Set the port that the event listener server will listen on."""
return await self._hub.set_port(port)
_LOGGER.debug("Setting event listener port to %s", port)
await self._hub.set_port(port)


class HubitatBase(ABC):
Expand Down
1 change: 1 addition & 0 deletions custom_components/hubitat/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,5 @@ async def async_setup_entry(
if is_light(devices[i])
]
async_add_entities(lights)
hub.add_entities(lights)
_LOGGER.debug(f"Added entities for lights: {lights}")
1 change: 1 addition & 0 deletions custom_components/hubitat/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async def async_setup_entry(
if CAP_LOCK in devices[i].capabilities
]
async_add_entities(locks)
hub.add_entities(locks)
_LOGGER.debug(f"Added entities for locks: %s", locks)

if len(locks) > 0:
Expand Down
1 change: 1 addition & 0 deletions custom_components/hubitat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ async def async_setup_entry(
if attr[0] in devices[i].attributes
]
async_add_entities(sensors)
hub.add_entities(sensors)
_LOGGER.debug(f"Added entities for sensors: {sensors}")
3 changes: 2 additions & 1 deletion custom_components/hubitat/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async def async_setup_entry(
else:
switches.append(HubitatSwitch(hub=hub, device=s))
async_add_entities(switches)
hub.add_entities(switches)
_LOGGER.debug("Added entities for switches: %s", switches)

button_controllers = [
Expand All @@ -107,5 +108,5 @@ async def async_setup_entry(
]
for bc in button_controllers:
hass.async_create_task(bc.update_device_registry())
hub.add_event_emitter(bc)
hub.add_event_emitters(button_controllers)
_LOGGER.debug("Added entities for pushbutton controllers: %s", button_controllers)

0 comments on commit 6eaf05d

Please sign in to comment.