1 """Interfaces with the myLeviton API for Decora Smart WiFi products."""
3 from __future__
import annotations
8 from decora_wifi
import DecoraWiFiSession
9 from decora_wifi.models.person
import Person
10 from decora_wifi.models.residence
import Residence
11 from decora_wifi.models.residential_account
import ResidentialAccount
12 import voluptuous
as vol
18 PLATFORM_SCHEMA
as LIGHT_PLATFORM_SCHEMA,
29 _LOGGER = logging.getLogger(__name__)
32 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
33 {vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_PASSWORD): cv.string}
36 NOTIFICATION_ID =
"leviton_notification"
37 NOTIFICATION_TITLE =
"myLeviton Decora Setup"
43 add_entities: AddEntitiesCallback,
44 discovery_info: DiscoveryInfoType |
None =
None,
46 """Set up the Decora WiFi platform."""
48 email = config[CONF_USERNAME]
49 password = config[CONF_PASSWORD]
50 session = DecoraWiFiSession()
53 success = session.login(email, password)
57 msg =
"Failed to log into myLeviton Services. Check credentials."
59 persistent_notification.create(
60 hass, msg, title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID
65 perms = session.user.get_residential_permissions()
66 all_switches: list = []
67 for permission
in perms:
68 if permission.residentialAccountId
is not None:
69 acct = ResidentialAccount(session, permission.residentialAccountId)
72 for residence
in acct.get_residences()
73 for switch
in residence.get_iot_switches()
75 elif permission.residenceId
is not None:
76 residence = Residence(session, permission.residenceId)
77 all_switches.extend(residence.get_iot_switches())
81 _LOGGER.error(
"Failed to communicate with myLeviton Service")
87 if session
is not None:
88 Person.logout(session)
90 _LOGGER.error(
"Failed to log out of myLeviton Service")
92 hass.bus.listen(EVENT_HOMEASSISTANT_STOP, logout)
96 """Representation of a Decora WiFi switch."""
99 """Initialize the switch."""
105 """Return the color mode of the light."""
106 if self.
_switch_switch.canSetLevel:
107 return ColorMode.BRIGHTNESS
108 return ColorMode.ONOFF
112 """Flag supported color modes."""
117 """Return supported features."""
118 if self.
_switch_switch.canSetLevel:
119 return LightEntityFeature.TRANSITION
124 """Return the display name of this switch."""
125 return self.
_switch_switch.name
129 """Return the ID of this light."""
130 return self.
_switch_switch.serial
134 """Return the brightness of the dimmer switch."""
135 return int(self.
_switch_switch.brightness * 255 / 100)
139 """Return true if switch is on."""
140 return self.
_switch_switch.power ==
"ON"
143 """Instruct the switch to turn on & adjust brightness."""
144 attribs: dict[str, Any] = {
"power":
"ON"}
146 if ATTR_BRIGHTNESS
in kwargs:
147 min_level = self.
_switch_switch.data.get(
"minLevel", 0)
148 max_level = self.
_switch_switch.data.get(
"maxLevel", 100)
149 brightness =
int(kwargs[ATTR_BRIGHTNESS] * max_level / 255)
150 brightness =
max(brightness, min_level)
151 attribs[
"brightness"] = brightness
153 if ATTR_TRANSITION
in kwargs:
154 transition =
int(kwargs[ATTR_TRANSITION])
155 attribs[
"fadeOnTime"] = attribs[
"fadeOffTime"] = transition
158 self.
_switch_switch.update_attributes(attribs)
160 _LOGGER.error(
"Failed to turn on myLeviton switch")
163 """Instruct the switch to turn off."""
164 attribs = {
"power":
"OFF"}
166 self.
_switch_switch.update_attributes(attribs)
168 _LOGGER.error(
"Failed to turn off myLeviton switch")
171 """Fetch new state data for this switch."""
175 _LOGGER.error(
"Failed to update myLeviton switch data")
set[str]|None supported_color_modes(self)
LightEntityFeature supported_features(self)
def __init__(self, switch)
None turn_on(self, **Any kwargs)
None turn_off(self, **Any kwargs)
ColorMode|str|None color_mode(self)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)