Skip to content

Commit

Permalink
Merge branch 'master' into frigate-notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegordon authored Jul 1, 2022
2 parents 363a6e1 + 4599b60 commit 3fa0b0f
Show file tree
Hide file tree
Showing 95 changed files with 7,884 additions and 1,554 deletions.
26 changes: 0 additions & 26 deletions binary_sensors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,3 @@
mdi:home-outline
{% endif %}
value_template: "{{ is_state('person.kyle','home') or is_state('person.charlotte','home') or is_state('person.ronnie','home') }}"

- platform: mqtt
name: Charlotte Bed Sensor
device_class: occupancy
state_topic: "devices/5ccf7fd8d915/Contact1/state"
payload_on: "1"
payload_off: "0"

- platform: mqtt
name: Kyle Bed Sensor
device_class: occupancy
state_topic: "devices/5ccf7fd8d915/Contact2/state"
payload_on: "1"
payload_off: "0"

- platform: mqtt
name: "Kyle Driving"
state_topic: "driving/mode/kyle"
payload_on: "true"
payload_off: "false"

- platform: mqtt
name: "Charlotte Driving"
state_topic: "driving/mode/charlotte"
payload_on: "true"
payload_off: "false"
16 changes: 0 additions & 16 deletions cameras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,3 @@
name: FrontDoor
still_image_url: http://viewpoint.house:4999/api/front_door/latest.jpg?h=400&motion=1
stream_source: rtmp://viewpoint.house/live/front_door

- name: Back Door Last Person
platform: mqtt
topic: frigate/back_door/person/snapshot
- name: Front Door Last Person
platform: mqtt
topic: frigate/front_door/person/snapshot
- name: Driveway Last Person
platform: mqtt
topic: frigate/driveway/person/snapshot
- name: Driveway Last Car
platform: mqtt
topic: frigate/driveway/car/snapshot
- name: octoPrint camera
platform: mqtt
topic: octoPrint/camera
29 changes: 1 addition & 28 deletions configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ deconz:
host: 172.24.32.13
port: 8100

google:
client_id: !secret google_oauth_client_id
client_secret: !secret google_oauth_client_secret
track_new_calendar: false

influxdb:
host: 172.24.32.13
port: 8086
Expand All @@ -159,10 +154,6 @@ influxdb:
# Discover some devices automatically
discovery:

spotify:
client_id: !secret spotify_client_id
client_secret: !secret spotify_client_secret

rfxtrx:
# Remember this is mapped in the docker-compose file
device: /dev/ttyUSB0
Expand Down Expand Up @@ -316,6 +307,7 @@ emulated_hue:
############
# automation: !include automation.yaml
# script: !include scripts.yaml
mqtt: !include mqtt.yaml
sensor: !include sensors.yaml
binary_sensor: !include binary_sensors.yaml
light: !include lights.yaml
Expand All @@ -338,25 +330,6 @@ input_select: !include_dir_named input_select
input_boolean: !include_dir_named input_boolean
script: !include_dir_merge_named scripts/

mqtt:
broker: 172.24.32.13
port: 1883
client_id: home-assistant-1
discovery: true
discovery_prefix: "homeassistant"
keepalive: 60
protocol: 3.1
birth_message:
topic: 'hass/status'
payload: 'online'
qos: 1
retain: true
will_message:
topic: 'hass/status'
payload: 'offline'
qos: 1
retain: true

utility_meter:
quarter_hourly_energy:
source: sensor.energy_spent
Expand Down
61 changes: 34 additions & 27 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@
from .const import (
ALEXA_COMPONENTS,
CONF_ACCOUNTS,
CONF_COOKIES_TXT,
CONF_DEBUG,
CONF_EXCLUDE_DEVICES,
CONF_EXTENDED_ENTITY_DISCOVERY,
CONF_INCLUDE_DEVICES,
CONF_OAUTH,
CONF_OAUTH_LOGIN,
CONF_OTPSECRET,
CONF_QUEUE_DELAY,
DATA_ALEXAMEDIA,
Expand Down Expand Up @@ -150,9 +148,12 @@ async def async_setup(hass, config, discovery_info=None):
CONF_SCAN_INTERVAL: account[
CONF_SCAN_INTERVAL
].total_seconds(),
CONF_OAUTH: account.get(CONF_OAUTH, {}),
CONF_OTPSECRET: account.get(CONF_OTPSECRET, ""),
CONF_OAUTH_LOGIN: account.get(CONF_OAUTH_LOGIN, True),
CONF_OAUTH: account.get(
CONF_OAUTH, entry.data.get(CONF_OAUTH, {})
),
CONF_OTPSECRET: account.get(
CONF_OTPSECRET, entry.data.get(CONF_OTPSECRET, "")
),
},
)
entry_found = True
Expand All @@ -173,7 +174,6 @@ async def async_setup(hass, config, discovery_info=None):
CONF_SCAN_INTERVAL: account[CONF_SCAN_INTERVAL].total_seconds(),
CONF_OAUTH: account.get(CONF_OAUTH, {}),
CONF_OTPSECRET: account.get(CONF_OTPSECRET, ""),
CONF_OAUTH_LOGIN: account.get(CONF_OAUTH_LOGIN, True),
},
)
)
Expand Down Expand Up @@ -218,12 +218,11 @@ async def relogin(event=None) -> None:
otp_secret=account.get(CONF_OTPSECRET, ""),
oauth=account.get(CONF_OAUTH, {}),
uuid=uuid,
oauth_login=bool(
account.get(CONF_OAUTH, {}).get("access_token")
or account.get(CONF_OAUTH_LOGIN)
),
oauth_login=True,
)
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["login_obj"] = login_obj
else:
login_obj.oauth_login = True
await login_obj.reset()
# await login_obj.login()
if await test_login_status(hass, config_entry, login_obj):
Expand Down Expand Up @@ -310,10 +309,7 @@ async def login_success(event=None) -> None:
otp_secret=account.get(CONF_OTPSECRET, ""),
oauth=account.get(CONF_OAUTH, {}),
uuid=uuid,
oauth_login=bool(
account.get(CONF_OAUTH, {}).get("access_token")
or account.get(CONF_OAUTH_LOGIN)
),
oauth_login=True,
),
)
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["login_obj"] = login
Expand All @@ -329,9 +325,7 @@ async def login_success(event=None) -> None:
return True
return False
except AlexapyConnectionError as err:
raise ConfigEntryNotReady(
str(err) or "Connection Error during login"
) from err
raise ConfigEntryNotReady(str(err) or "Connection Error during login") from err


async def setup_alexa(hass, config_entry, login_obj: AlexaLogin):
Expand Down Expand Up @@ -622,7 +616,7 @@ async def async_update_data() -> Optional[AlexaEntityData]:

hass.data[DATA_ALEXAMEDIA]["accounts"][email]["new_devices"] = False
# prune stale devices
device_registry = await dr.async_get_registry(hass)
device_registry = dr.async_get(hass)
for device_entry in dr.async_entries_for_config_entry(
device_registry, config_entry.entry_id
):
Expand Down Expand Up @@ -652,6 +646,7 @@ async def async_update_data() -> Optional[AlexaEntityData]:
"access_token": login_obj.access_token,
"refresh_token": login_obj.refresh_token,
"expires_in": login_obj.expires_in,
"mac_dms": login_obj.mac_dms,
},
},
)
Expand All @@ -662,7 +657,7 @@ async def process_notifications(login_obj, raw_notifications=None):
"""Process raw notifications json."""
if not raw_notifications:
raw_notifications = await AlexaAPI.get_notifications(login_obj)
email: Text = login_obj.email
email: str = login_obj.email
previous = hass.data[DATA_ALEXAMEDIA]["accounts"][email].get(
"notifications", {}
)
Expand Down Expand Up @@ -802,6 +797,7 @@ async def ws_connect() -> WebsocketEchoClient:
This will only attempt one login before failing.
"""
websocket: Optional[WebsocketEchoClient] = None
email = login_obj.email
try:
if login_obj.session.closed:
_LOGGER.debug(
Expand All @@ -818,6 +814,17 @@ async def ws_connect() -> WebsocketEchoClient:
)
_LOGGER.debug("%s: Websocket created: %s", hide_email(email), websocket)
await websocket.async_run()
except AlexapyLoginError as exception_:
_LOGGER.debug(
"%s: Login Error detected from websocket: %s",
hide_email(email),
exception_,
)
hass.bus.async_fire(
"alexa_media_relogin_required",
event_data={"email": hide_email(email), "url": login_obj.url},
)
return
except BaseException as exception_: # pylint: disable=broad-except
_LOGGER.debug(
"%s: Websocket creation failed: %s", hide_email(email), exception_
Expand Down Expand Up @@ -989,6 +996,7 @@ async def ws_handler(message_obj):
"PUSH_LIST_ITEM_CHANGE", # update shopping list
"PUSH_CONTENT_FOCUS_CHANGE", # likely prime related refocus
"PUSH_DEVICE_SETUP_STATE_CHANGE", # likely device changes mid setup
"PUSH_MEDIA_PREFERENCE_CHANGE", # disliking or liking songs, https://github.com/custom-components/alexa_media_player/issues/1599
]:
pass
else:
Expand Down Expand Up @@ -1051,7 +1059,7 @@ async def ws_handler(message_obj):
async def ws_open_handler():
"""Handle websocket open."""

email: Text = login_obj.email
email: str = login_obj.email
_LOGGER.debug("%s: Websocket successfully connected", hide_email(email))
hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"websocketerror"
Expand All @@ -1066,7 +1074,7 @@ async def ws_close_handler():
This should attempt to reconnect up to 5 times
"""

email: Text = login_obj.email
email: str = login_obj.email
if login_obj.close_requested:
_LOGGER.debug(
"%s: Close requested; will not reconnect websocket", hide_email(email)
Expand All @@ -1078,7 +1086,7 @@ async def ws_close_handler():
)
return
errors: int = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
delay: int = 5 * 2 ** errors
delay: int = 5 * 2**errors
last_attempt = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"websocket_lastattempt"
]
Expand All @@ -1104,7 +1112,7 @@ async def ws_close_handler():
errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = (
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] + 1
)
delay = 5 * 2 ** errors
delay = 5 * 2**errors
errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
await asyncio.sleep(delay)
if not websocket_enabled:
Expand All @@ -1125,7 +1133,7 @@ async def ws_error_handler(message):
the websocket and determine if a reconnect should be done. By
specification, websockets will issue a close after every error.
"""
email: Text = login_obj.email
email: str = login_obj.email
errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
_LOGGER.debug(
"%s: Received websocket error #%i %s: type %s",
Expand Down Expand Up @@ -1242,7 +1250,7 @@ async def async_unload_entry(hass, entry) -> bool:
return True


async def close_connections(hass, email: Text) -> None:
async def close_connections(hass, email: str) -> None:
"""Clear open aiohttp connections for email."""
if (
email not in hass.data[DATA_ALEXAMEDIA]["accounts"]
Expand Down Expand Up @@ -1290,7 +1298,7 @@ async def test_login_status(hass, config_entry, login) -> bool:
account = config_entry.data
_LOGGER.debug("Logging in: %s %s", obfuscate(account), in_progess_instances(hass))
_LOGGER.debug("Login stats: %s", login.stats)
message: Text = f"Reauthenticate {login.email} on the [Integrations](/config/integrations) page. "
message: str = f"Reauthenticate {login.email} on the [Integrations](/config/integrations) page. "
if login.stats.get("login_timestamp") != datetime(1, 1, 1):
elaspsed_time: str = str(datetime.now() - login.stats.get("login_timestamp"))
api_calls: int = login.stats.get("api_calls")
Expand Down Expand Up @@ -1331,7 +1339,6 @@ async def test_login_status(hass, config_entry, login) -> bool:
CONF_SCAN_INTERVAL: account[CONF_SCAN_INTERVAL].total_seconds()
if isinstance(account[CONF_SCAN_INTERVAL], timedelta)
else account[CONF_SCAN_INTERVAL],
CONF_COOKIES_TXT: account.get(CONF_COOKIES_TXT, ""),
CONF_OTPSECRET: account.get(CONF_OTPSECRET, ""),
},
)
Expand Down
Loading

0 comments on commit 3fa0b0f

Please sign in to comment.