RobertD502 / home-assistant-flair

Custom component for Home Assistant Core for Flair pucks, vents, rooms, structures, and minisplits
MIT License
87 stars 12 forks source link

Flooding API DNS Requests #24

Closed Lurick73 closed 2 years ago

Lurick73 commented 2 years ago

I recently enabled this plugin and within 24 hours I had almost 450,000 DNS queries to api.flair.com. I'm hoping there is a way to throttle this WAY back to something like 1 or 2 every few seconds. I don't think that many API queries need to go out in under a day as that seems astronomically high. Number of vents: 5 Number of pucks: 1 Plugin Version: 0.0.5.7 Home Assistant Version: 2022.5.5

RobertD502 commented 2 years ago

Every Puck, Room, Vent, Structure, HVAC unit, Schedules, etc has its own endpoint. A Flair structure's endpoint lists everything in it such as Rooms, Pucks, Vents, and Structure data. However, the info provided at this endpoint doesn't have all of the required information needed - this is why Flair has individual endpoints for each device, room, etc. The way Flair has structured its API results in needing to make multiple requests - how many depends on the amount of pucks, rooms, vents, HVAC units within a user's Flair structure. I can't change this as I don't have control over Flair's API design.

The integration sends requests to Flair's servers every 30 seconds, which is the default update interval set by home assistant. If you want to change this to a different value you will need to manually edit "climate.py", "cover.py", "select.py", and "sensor.py".

Within each one of those files, immediately under the line that reads from .const import Domain, you'll need to add this:


from datetime import timedelta

SCAN_INTERVAL = timedelta(seconds=60)

Replace 60 with however often you want the entities to update by polling Flair's API.

Again, I can't change the total requests being sent as this is related to how Flair structured its API, but you can change how often you want those requests to be made. It would make the backend code the integration uses simpler if Flair opted to present all available data for devices/rooms within a Structure's API endpoint, but we have to work with what we are given.

Lurick73 commented 2 years ago

Awesome, I can work with that. I wasn't exactly sure if it was the integration causing issues or what but if that's what you're given to work with that's fine. I'll play around with some tweaks and see if I can lighten the request load a bit, thanks!

RobertD502 commented 2 years ago

Just wanted to add: You should never be seeing 450,000 requests in a single 24 hour period from this integration based on your setup and the default 30s update interval. I'd investigate those DNS queries to see if you were experiencing some networking issues that were resulting in retries.

poindexter12 commented 2 years ago

I think there might be something else at the root cause here. I'm having the same issue where this is querying my DNS (PiHole) every second, with there being multiple queries during that second. I'm not sure what the root cause here is but I'm not sure if it's just that setting. Attached is a screenshot. I'm curious if @Lurick73 has the same setup.

Screen Shot 2022-06-14 at 8 45 08 AM

Lurick73 commented 2 years ago

@poindexter12 Actually that's exactly the setup I have, even after adjusting the timers to 120 seconds per the above I was still getting around 100k queries to api.flair.co

RobertD502 commented 2 years ago

Could it have something to do with the PiHole? I use Adguard Home in my setup and don't have this issue.

poindexter12 commented 2 years ago

I would think so as the PiHole is just my first line DNS resolver so I can see all the requests going to it. Does Adguard allow you to see all of the DNS queries? I did some looking through the code as well and I'm wondering if there's something that the HA framework is doing under the covers based on responses.

RobertD502 commented 2 years ago

I would think so as the PiHole is just my first line DNS resolver so I can see all the requests going to it. Does Adguard allow you to see all of the DNS queries? I did some looking through the code as well and I'm wondering if there's something that the HA framework is doing under the covers based on responses.

Is there ever a 30s break in the log (if you left the default update interval) between queries or is it a constant every second query with no breaks?

Adguard Home does allow you to see every query. To be absolutely positive of my count, I went as far as fetching my entire log file and filtering it for a single day to get the count which was nowhere close to the values you are experiencing.

poindexter12 commented 2 years ago

@Lurick73 is your home assistant in a container as well? one thing I'm seeing in pihole is that MOST of the queries are coming from my main host and the smaller, expected amount, is coming just from the internal docker network I have.

Lurick73 commented 2 years ago

@poindexter12 It's running on a Ubuntu VM (HA Core I think is the right term) but not in a docker container or anything at this point.