Skip to content

Commit

Permalink
Merge pull request #338
Browse files Browse the repository at this point in the history
* global local control
  • Loading branch information
brg468 authored Feb 16, 2022
1 parent e524ecc commit 33e010b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
9 changes: 8 additions & 1 deletion custom_components/wyzeapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from wyzeapy import Wyzeapy
from wyzeapy.wyze_auth_lib import Token

from .const import DOMAIN, CONF_CLIENT, ACCESS_TOKEN, REFRESH_TOKEN, REFRESH_TIME, WYZE_NOTIFICATION_TOGGLE
from .const import (
DOMAIN, CONF_CLIENT, ACCESS_TOKEN, REFRESH_TOKEN,
REFRESH_TIME, WYZE_NOTIFICATION_TOGGLE, BULB_LOCAL_CONTROL,
DEFAULT_LOCAL_CONTROL
)
from .token_manager import TokenManager

PLATFORMS = [
Expand Down Expand Up @@ -106,6 +110,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b

hass.data[DOMAIN][config_entry.entry_id] = {CONF_CLIENT: client}

options_dict = {BULB_LOCAL_CONTROL: config_entry.options.get(BULB_LOCAL_CONTROL, DEFAULT_LOCAL_CONTROL)}
hass.config_entries.async_update_entry(config_entry, options=options_dict)

for platform in PLATFORMS:
hass.create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
Expand Down
36 changes: 35 additions & 1 deletion custom_components/wyzeapi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_ACCESS_TOKEN
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
from wyzeapy import Wyzeapy, exceptions

from .const import DOMAIN, ACCESS_TOKEN, REFRESH_TOKEN, REFRESH_TIME
from .const import (
DOMAIN, ACCESS_TOKEN, REFRESH_TOKEN,
REFRESH_TIME, BULB_LOCAL_CONTROL,
DEFAULT_LOCAL_CONTROL
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -116,6 +121,35 @@ async def async_step_reauth(self, user_input=None):
)
return await self.async_step_user()

@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)


class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle an option flow for Wyze."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
"""Handle options flow."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

data_schema = vol.Schema(
{
vol.Optional(
BULB_LOCAL_CONTROL,
default=self.config_entry.options.get(BULB_LOCAL_CONTROL, DEFAULT_LOCAL_CONTROL)
): bool
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)


class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""
Expand Down
3 changes: 3 additions & 0 deletions custom_components/wyzeapi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
CAMERA_UPDATED = f"{DOMAIN}.camera_updated"
# EVENT NAMES
WYZE_CAMERA_EVENT = "wyze_camera_event"

BULB_LOCAL_CONTROL = "bulb_local_control"
DEFAULT_LOCAL_CONTROL = True
32 changes: 20 additions & 12 deletions custom_components/wyzeapi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from wyzeapy.types import DeviceTypes, PropertyIDs
from wyzeapy.utils import create_pid_pair

from .const import DOMAIN, CONF_CLIENT
from .const import DOMAIN, CONF_CLIENT, BULB_LOCAL_CONTROL
from .token_manager import token_exception_handler

_LOGGER = logging.getLogger(__name__)
Expand All @@ -49,7 +49,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry,

bulb_service = await client.bulb_service

lights = [WyzeLight(bulb_service, light) for light in await bulb_service.get_bulbs()]
lights = [WyzeLight(bulb_service, light, config_entry) for light in await bulb_service.get_bulbs()]

async_add_entities(lights, True)

Expand All @@ -61,10 +61,12 @@ class WyzeLight(LightEntity):

_just_updated = False

def __init__(self, bulb_service: BulbService, bulb: Bulb):
def __init__(self, bulb_service: BulbService, bulb: Bulb, config_entry):
"""Initialize a Wyze Bulb."""
self._bulb = bulb
self._device_type = DeviceTypes(self._bulb.product_type)
self._config_entry = config_entry
self._local_control = config_entry.options.get(BULB_LOCAL_CONTROL)
if self._device_type not in [
DeviceTypes.LIGHT,
DeviceTypes.MESH_LIGHT,
Expand Down Expand Up @@ -131,17 +133,19 @@ async def async_turn_on(self, **kwargs: Any) -> None:
self._bulb.color = color

_LOGGER.debug("Turning on light")
self._local_control = self._config_entry.options.get(BULB_LOCAL_CONTROL)
loop = asyncio.get_event_loop()
loop.create_task(self._bulb_service.turn_on(self._bulb, options))
loop.create_task(self._bulb_service.turn_on(self._bulb, self._local_control, options))

self._bulb.on = True
self._just_updated = True
self.async_schedule_update_ha_state()

@token_exception_handler
async def async_turn_off(self, **kwargs: Any) -> None:
self._local_control = self._config_entry.options.get(BULB_LOCAL_CONTROL)
loop = asyncio.get_event_loop()
loop.create_task(self._bulb_service.turn_off(self._bulb))
loop.create_task(self._bulb_service.turn_off(self._bulb, self._local_control))

self._bulb.on = False
self._just_updated = True
Expand Down Expand Up @@ -172,16 +176,10 @@ def extra_state_attributes(self):
ATTR_ATTRIBUTION: ATTRIBUTION,
"state": self.is_on,
"available": self.available,
"device model": self._bulb.product_model,
"device_model": self._bulb.product_model,
"mac": self.unique_id
}

if (
self._device_type is DeviceTypes.MESH_LIGHT
or self._device_type is DeviceTypes.LIGHTSTRIP
):
dev_info["Cloud"] = self._bulb.cloud_fallback

# noinspection DuplicatedCode
if self._bulb.device_params.get("ip"):
dev_info["IP"] = str(self._bulb.device_params.get("ip"))
Expand All @@ -190,6 +188,15 @@ def extra_state_attributes(self):
if self._bulb.device_params.get("ssid"):
dev_info["SSID"] = str(self._bulb.device_params.get("ssid"))

if (
self._device_type is DeviceTypes.MESH_LIGHT
or self._device_type is DeviceTypes.LIGHTSTRIP
):
dev_info["local_control"] = (
self._local_control
and not self._bulb.cloud_fallback
)

return dev_info

@property
Expand Down Expand Up @@ -245,6 +252,7 @@ async def async_update(self):
def async_update_callback(self, bulb: Bulb):
"""Update the bulb's state."""
self._bulb = bulb
self._local_control = self._config_entry.options.get(BULB_LOCAL_CONTROL)
self.async_schedule_update_ha_state()

async def async_added_to_hass(self) -> None:
Expand Down
5 changes: 5 additions & 0 deletions custom_components/wyzeapi/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
},
"options": {
"step": {
"init":{
"data":{
"bulb_local_control": "Use Local Control for Color Bulbs and Light Strips"
}
},
"user": {
"title": "[%key:common::config_flow::data::title%]",
"data": {
Expand Down
5 changes: 5 additions & 0 deletions custom_components/wyzeapi/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"unknown": "Unexpected error"
},
"step": {
"init":{
"data":{
"bulb_local_control": "Use Local Control for Color Bulbs and Light Strips"
}
},
"user": {
"title": "Enter Wyze Login Credentials",
"data": {
Expand Down

0 comments on commit 33e010b

Please sign in to comment.