Skip to content

Commit

Permalink
Merge pull request #9 from konnected-io/using_const_py
Browse files Browse the repository at this point in the history
changing constants to use const.py from core
  • Loading branch information
heythisisnate authored Aug 26, 2022
2 parents 8ddee30 + a947643 commit d2ba87c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 62 deletions.
12 changes: 7 additions & 5 deletions homeassistant/components/konnected/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
from homeassistant.components.http import HomeAssistantView
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_ACCESS_TOKEN,
CONF_BINARY_SENSORS,
CONF_DEVICE_ID,
CONF_DEVICES,
CONF_DISCOVERY,
CONF_ENTITY_ID,
CONF_HOST,
CONF_ID,
CONF_NAME,
CONF_PIN,
CONF_PORT,
CONF_REPEAT,
CONF_SENSORS,
CONF_STATE,
CONF_SWITCHES,
CONF_TYPE,
CONF_ZONE,
Expand Down Expand Up @@ -365,7 +367,7 @@ async def update_sensor(self, request: Request, device_id) -> Response:
"unregistered sensor/actuator", status_code=HTTPStatus.BAD_REQUEST
)

zone_data["device_id"] = device_id
zone_data[CONF_DEVICE_ID] = device_id

for attr in ("state", "temp", "humi", "addr"):
value = payload.get(attr)
Expand Down Expand Up @@ -423,14 +425,14 @@ async def get(self, request: Request, device_id) -> Response:
resp[CONF_PIN] = ZONE_TO_PIN[zone_num]

# Make sure entity is setup
if zone_entity_id := zone.get(ATTR_ENTITY_ID):
resp["state"] = self.binary_value(
if zone_entity_id := zone.get(CONF_ENTITY_ID):
resp[CONF_STATE] = self.binary_value(
hass.states.get(zone_entity_id).state, zone[CONF_ACTIVATION]
)
return self.json(resp)

_LOGGER.warning("Konnected entity not yet setup, returning default")
resp["state"] = self.binary_value(STATE_OFF, zone[CONF_ACTIVATION])
resp[CONF_STATE] = self.binary_value(STATE_OFF, zone[CONF_ACTIVATION])
return self.json(resp)

async def put(self, request: Request, device_id) -> Response:
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/konnected/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_STATE,
CONF_BINARY_SENSORS,
CONF_DEVICES,
CONF_ENTITY_ID,
CONF_NAME,
CONF_TYPE,
)
Expand Down Expand Up @@ -81,7 +81,7 @@ def device_info(self) -> DeviceInfo:

async def async_added_to_hass(self):
"""Store entity_id and register state change callback."""
self._data[ATTR_ENTITY_ID] = self.entity_id
self._data[CONF_ENTITY_ID] = self.entity_id
self.async_on_remove(
async_dispatcher_connect(
self.hass, f"konnected.{self.entity_id}.update", self.async_set_state
Expand Down
59 changes: 31 additions & 28 deletions homeassistant/components/konnected/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
)
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_BASE,
CONF_BINARY_SENSORS,
CONF_DISCOVERY,
CONF_HOST,
CONF_ID,
CONF_MAC,
CONF_MODEL,
CONF_NAME,
CONF_PORT,
CONF_REPEAT,
CONF_SENSORS,
CONF_STATE,
CONF_SWITCHES,
CONF_TYPE,
CONF_ZONE,
Expand Down Expand Up @@ -188,11 +191,11 @@ async def async_gen_config(self, host, port):
self.data[CONF_PORT] = port
try:
status = await get_status(self.hass, host, port)
self.data[CONF_ID] = status.get("chipId", status["mac"].replace(":", ""))
self.data[CONF_ID] = status.get("chipId", status[CONF_MAC].replace(":", ""))
except (CannotConnect, KeyError) as err:
raise CannotConnect from err
else:
self.data[CONF_MODEL] = status.get("model", KONN_MODEL)
self.data[CONF_MODEL] = status.get(CONF_MODEL, KONN_MODEL)
self.data[CONF_ACCESS_TOKEN] = "".join(
random.choices(f"{string.ascii_uppercase}{string.digits}", k=20)
)
Expand Down Expand Up @@ -286,9 +289,9 @@ async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowRes
self.data[CONF_HOST] = netloc[0]
self.data[CONF_PORT] = int(netloc[1])
self.data[CONF_ID] = status.get(
"chipId", status["mac"].replace(":", "")
"chipId", status[CONF_MAC].replace(":", "")
)
self.data[CONF_MODEL] = status.get("model", KONN_MODEL)
self.data[CONF_MODEL] = status.get(CONF_MODEL, KONN_MODEL)

KonnectedFlowHandler.discovered_hosts[self.data[CONF_ID]] = {
CONF_HOST: self.data[CONF_HOST],
Expand All @@ -313,12 +316,12 @@ async def async_step_user(self, user_input=None):
self.hass, self.data[CONF_HOST], self.data[CONF_PORT]
)
except CannotConnect:
errors["base"] = "cannot_connect"
errors[CONF_BASE] = "cannot_connect"
else:
self.data[CONF_ID] = status.get(
"chipId", status["mac"].replace(":", "")
"chipId", status[CONF_MAC].replace(":", "")
)
self.data[CONF_MODEL] = status.get("model", KONN_MODEL)
self.data[CONF_MODEL] = status.get(CONF_MODEL, KONN_MODEL)

# save off our discovered host info
KonnectedFlowHandler.discovered_hosts[self.data[CONF_ID]] = {
Expand All @@ -330,8 +333,8 @@ async def async_step_user(self, user_input=None):
return self.async_show_form(
step_id="user",
description_placeholders={
"host": self.data.get(CONF_HOST, "Unknown"),
"port": self.data.get(CONF_PORT, "Unknown"),
CONF_HOST: self.data.get(CONF_HOST, "Unknown"),
CONF_PORT: self.data.get(CONF_PORT, "Unknown"),
},
data_schema=vol.Schema(
{
Expand All @@ -357,10 +360,10 @@ async def async_step_confirm(self, user_input=None):
return self.async_show_form(
step_id="confirm",
description_placeholders={
"model": KONN_PANEL_MODEL_NAMES[self.data[CONF_MODEL]],
"id": self.unique_id,
"host": self.data[CONF_HOST],
"port": self.data[CONF_PORT],
CONF_MODEL: KONN_PANEL_MODEL_NAMES[self.data[CONF_MODEL]],
CONF_ID: self.unique_id,
CONF_HOST: self.data[CONF_HOST],
CONF_PORT: self.data[CONF_PORT],
},
)

Expand Down Expand Up @@ -457,8 +460,8 @@ async def async_step_options_io(self, user_input=None):
}
),
description_placeholders={
"model": KONN_PANEL_MODEL_NAMES[self.model],
"host": self.entry.data[CONF_HOST],
CONF_MODEL: KONN_PANEL_MODEL_NAMES[self.model],
CONF_HOST: self.entry.data[CONF_HOST],
},
errors=errors,
)
Expand Down Expand Up @@ -493,8 +496,8 @@ async def async_step_options_io(self, user_input=None):
}
),
description_placeholders={
"model": KONN_PANEL_MODEL_NAMES[self.model],
"host": self.entry.data[CONF_HOST],
CONF_MODEL: KONN_PANEL_MODEL_NAMES[self.model],
CONF_HOST: self.entry.data[CONF_HOST],
},
errors=errors,
)
Expand Down Expand Up @@ -551,8 +554,8 @@ async def async_step_options_io_ext(self, user_input=None):
}
),
description_placeholders={
"model": KONN_PANEL_MODEL_NAMES[self.model],
"host": self.entry.data[CONF_HOST],
CONF_MODEL: KONN_PANEL_MODEL_NAMES[self.model],
CONF_HOST: self.entry.data[CONF_HOST],
},
errors=errors,
)
Expand Down Expand Up @@ -592,7 +595,7 @@ async def async_step_options_binary(self, user_input=None):
}
),
description_placeholders={
"zone": f"Zone {self.active_cfg}"
CONF_ZONE: f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper
},
Expand Down Expand Up @@ -663,7 +666,7 @@ async def async_step_options_digital(self, user_input=None):
}
),
description_placeholders={
"zone": f"Zone {self.active_cfg}"
CONF_ZONE: f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper()
},
Expand Down Expand Up @@ -693,7 +696,7 @@ async def async_step_options_digital(self, user_input=None):
}
),
description_placeholders={
"zone": f"Zone {self.active_cfg}"
CONF_ZONE: f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper()
},
Expand All @@ -706,7 +709,7 @@ async def async_step_options_switch(self, user_input=None):
"""Allow the user to configure the IO options for switches."""
errors = {}
if user_input is not None:
zone = {"zone": self.active_cfg}
zone = {CONF_ZONE: self.active_cfg}
zone.update(user_input)
del zone[CONF_MORE_STATES]
self.new_opt[CONF_SWITCHES] = self.new_opt.get(CONF_SWITCHES, []) + [zone]
Expand Down Expand Up @@ -755,10 +758,10 @@ async def async_step_options_switch(self, user_input=None):
}
),
description_placeholders={
"zone": f"Zone {self.active_cfg}"
CONF_ZONE: f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper(),
"state": str(self.current_state),
CONF_STATE: str(self.current_state),
},
errors=errors,
)
Expand Down Expand Up @@ -807,10 +810,10 @@ async def async_step_options_switch(self, user_input=None):
}
),
description_placeholders={
"zone": f"Zone {self.active_cfg}"
CONF_ZONE: f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper(),
"state": str(self.current_state),
CONF_STATE: str(self.current_state),
},
errors=errors,
)
Expand All @@ -828,7 +831,7 @@ async def async_step_options_misc(self, user_input=None):
else:
user_input[CONF_API_HOST] = ""
except vol.Invalid:
errors["base"] = "bad_host"
errors[CONF_BASE] = "bad_host"
else:
# no need to store the override - can infer
del user_input[CONF_OVERRIDE_API_HOST]
Expand Down
16 changes: 11 additions & 5 deletions homeassistant/components/konnected/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import logging

from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE
from homeassistant.const import (
ATTR_DEVICE_ID,
ATTR_STATE,
ATTR_TEMPERATURE,
CONF_ENTITY_ID,
CONF_STATE,
)
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.util import decorator

Expand All @@ -12,11 +18,11 @@
HANDLERS = decorator.Registry() # type: ignore[var-annotated]


@HANDLERS.register("state")
@HANDLERS.register(CONF_STATE)
async def async_handle_state_update(hass, context, msg):
"""Handle a binary sensor or switch state update."""
_LOGGER.debug("[state handler] context: %s msg: %s", context, msg)
entity_id = context.get(ATTR_ENTITY_ID)
entity_id = context.get(CONF_ENTITY_ID)
state = bool(int(msg.get(ATTR_STATE)))
if context.get(CONF_INVERSE):
state = not state
Expand Down Expand Up @@ -50,7 +56,7 @@ async def async_handle_addr_update(hass, context, msg):
if entity_id := context.get(addr):
async_dispatcher_send(hass, f"konnected.{entity_id}.update", temp)
else:
msg["device_id"] = context.get("device_id")
msg["temperature"] = temp
msg[ATTR_DEVICE_ID] = context.get(ATTR_DEVICE_ID)
msg[ATTR_TEMPERATURE] = temp
msg["addr"] = addr
async_dispatcher_send(hass, SIGNAL_DS18B20_NEW, msg)
Loading

0 comments on commit d2ba87c

Please sign in to comment.