Friday 26 August 2022

 Books on Monterey - A Followup

In a previous post, I discussed what was wrong with Books for Monterey.  I've been trying to use it since then and, as of today, have given up on the Mac version.  Why?
  1. I lost all of my books on the iCloud.  Every one.  iCloud backup was of no use, and I opened a call to apple about this.  After 2 months they still haven't been able to recover them - see below.
  2. If you reach for a book and want information on it, and select the 3 dots on the right to look at the "info" to it,  It supplies the cover, title and author.  No collection, date, publisher etc., or any other information.
  3. You can't find which collections a book is contained in without going to each collection and looking for it.
  4. Collections don' t show the numbers of items in them (as on the iPad version)
I do still use Books, but only on the iPad, but I organise them differently now.

I use Calibre to organise the books I want, tag them etc and then add them to Books when I'm completed.  I use the calibre library and a couple of perl scripts to make sure everything is consistent between calibre and Books.  I would like to be able to create collections via the tags in calibre, but that's for another day.

Audiobooks are still really broken, and I still use Plex and Prologue to listen to them.

Lost iCloud Books

After I installed a new Mac mini, all of my books (over 1500) on iCloud vanished.  On all my devices, totally.

I opened up a call to Apple about this and, as of 2 months, they have not been able to recover them.  The iCloud Drive does *not* save your books.  They're in some strange place.

The local copies of all books are stored in 

<Home directory>/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents/


However, they are not all there.  If the file starts "period"-name.icloud it's just a reference to the file on iCloud and it needs to be downloaded before you can read it.

e.g:.

Backblaze jimmy$ ls -l .1Password\ for\ iOS.epub.icloud 

-rw-r--r--  1 jimmy  staff  173 16 Jan  2016 .1Password for iOS.epub.icloud

Jimmys-Mac:Backblaze jimmy$ hexdump -C .1Password\ for\ iOS.epub.icloud

00000000  62 70 6c 69 73 74 30 30  d3 01 02 03 04 05 06 5c  |bplist00?......\|

00000010  4e 53 55 52 4c 4e 61 6d  65 4b 65 79 5f 10 10 4e  |NSURLNameKey_..N|

00000020  53 55 52 4c 46 69 6c 65  53 69 7a 65 4b 65 79 5f  |SURLFileSizeKey_|

00000030  10 18 4e 53 55 52 4c 46  69 6c 65 52 65 73 6f 75  |..NSURLFileResou|

00000040  72 63 65 54 79 70 65 4b  65 79 5f 10 16 31 50 61  |rceTypeKey_..1Pa|

00000050  73 73 77 6f 72 64 20 66  6f 72 20 69 4f 53 2e 65  |ssword for iOS.e|

00000060  70 75 62 10 00 5f 10 1e  4e 53 55 52 4c 46 69 6c  |pub.._..NSURLFil|

00000070  65 52 65 73 6f 75 72 63  65 54 79 70 65 44 69 72  |eResourceTypeDir|

00000080  65 63 74 6f 72 79 08 0f  1c 2f 4a 63 65 00 00 00  |ectory.../Jce...|

00000090  00 00 00 01 01 00 00 00  00 00 00 00 07 00 00 00  |................|

000000a0  00 00 00 00 00 00 00 00  00 00 00 00 86           |.............|

000000ad

Backblaze jimmy$ plutil -p - < .1Password\ for\ iOS.epub.icloud

{

  "NSURLFileResourceTypeKey" => "NSURLFileResourceTypeDirectory"

  "NSURLFileSizeKey" => 0

  "NSURLNameKey" => "1Password for iOS.epub"

}


If you open up the enclosing file in finder, it lies to you about the files and says it really exists.  Don't believe it.  Finder is lying.

I managed to recover about 1000 from my Backblaze backup, and old disk, a Mac that I hadn't turned on since the files were lost and another Carbon Copy Cloner backup..  The rest I've having to look for slowly on old disks, email, purchases etc.

Tuesday 15 March 2022

Dtrace on Monterey

 As I mentioned in a previous post, dtrace was broken in El Capitan and later, but all is not yet lost.  As a post on medium has described, there are other ways around this including using approved methods from Apple.  In normal cases, non system binaries cannot be examined with dtrace.

Chris demonstrates a way to run dtrace in a chroot-ed environment after turning off code signing to allow and common to be run with dtrace as well as any children forked off. 

The officially sanctioned methods of getting security framework events ( not the same as sys call level output) is used be the following programs and can be used to trace some of what dtrace can do, but without the faff:

  1. Filemonitor (from Objective See)

Print JSON file on system events pertaining to files.

  1. Processmonitor (from Objective See)

Same as above, but for processes

  1. Crescendo ( project on GitHub)

Crescendo is a swift based, GUI real time event viewer for macOS. It utilizes Apple's Endpoint Security Framework.


So there is a. bit more hope now... 


Tuesday 22 February 2022

MacOS 12 Monterey

 Thoughts on installing Monterey and migrating from Mojave

Music

OK, so everything was asking for iTunes to be split up into different applications, and this is the result.  It now deals with just music, and it's pretty my the same.  I had a few scripts from Doug Scripts which I'll need to change/update to work with this but it looks OK.

Books

Great Maker, what have they done.  This is just awful.  Been running for 2 days and these are my gripes:
  1. It's basically an iPad application, let's be honest
  2. The list view is not a list.  It's an icon with a title.
  3. You can't get information on the files. 'Get Info' retried the title, the author and possibly the cover.  You can't do anything with then, and it's just what you see normally, so what's the point. 
  4. It's now the ONLY way to listen to audiobooks.  Previously iTunes stored audio books and could play them.  Now I need to migrate all of the audio books into The Books app.
    1. It combines all the files into one title now.  I save a lot of radio plays/episode and this is just horrible.  You can't see home may chapters are in there or re-tag them (sine they are mpeg4 audio files, there are a lot of tags),
    2. You can't view all of the tags like, author, description, comment, lyrics, date, Disc/track (which are useful for radio series)
    3. The files are stored in ~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/Audiobooks and tagged by a sha1-checksum and not the title, so it's impossible to work out what you have via the filename (the same is true for normal books, but it's still a change from the old method)
I'll be migrating to Plex audio and use Prologue on my iPhone to listen from now on.  I've been using it for a while, on and off, but not this has really convinced me to drop the Apple Ecosystem for my Audio Books/Dramas for certain.  I'll need to change my workflow, but I'll manage.  It used to be 'add to iTunes' and then let Plex look for audiobooks.  Now it'll need to be a bit more involved.  A challenge.

Workaround

To get some semblance or normality for books:
  1. Downloaded the Catalina Install image from the App Store.
  2. Created a Virtual Machine using VMware Fusion and installed it there.
  3. Did almost no setup etc, just to get it running; No Apples ID or anything.
  4. Installed VM Ware tools to I could access it better.
  5. Located the Binary for Books.app under /Applications and copied it to my Monterey Macintosh and called it 'Cata-Books' instead.
  6. Run 'Cata-Books' instead under Monterey to get the old functionality back.
  7. Deleted the Catalina VM since it's no longer needed.
  8. Breathe again.

AudioBooks

Take everything I said about Books and keep it here, then multiply by a factor.  Whereas previously I could see my Audiobook, edit some metadata, Add new episodes (for radio dramas and comedies), now it's all held under the "improved" Books program.  Can't edit meta-data, can't easily see how the books may be split up into individual files.  Most importantly, I can't specify a separate area to store these like I do for TV and Music.  They *have* to be under ~/Library unless I try something clever with symlinks and external volumes which I really do not want to do.

https://macmost.com/how-to-manage-an-audiobook-collection-in-macos-catalina.html has a good article on this.  Myself, I going to write some code to add files to the correct place based on the metadata tagged, and then  use a combination of Plex and prologue to listen on my iPhone/Ipad.  I started to do this anyway, so I just need to modify the code I wrote to add to iTunes to write to the same place as iTunes used to and they'll be picked up by Plex.  Although Plex isn't that great on searching.

Checkout the following articles as well
I use MetaZ installed via homebrew to work on tags as well as my own programs to edit the tags.

Come back iTunes.

TV/Movies

Same gripes for iTunes.  List mode is a bit off and you can't separate Downloaded items versus items in the Cloud which is a bit annoying, but not a deal breaker.

PDF support

It looks like you can no longer get around the issue of protected PDFs.  My workflow used to be to get a PDF and Print to Paperless to archive it off to the Paperless program. Under older releases you could:
  1. Open the file in Preview
  2. Export it as another PDF
  3. Close preview.
  4. Open the newly created file and then ue the 'Print to Paperless' option or open it withPDFPen and the like.
None of this works any more, so to unprotect a PDF, I need to fire up an non Monterey VM, copy the file in to perform the above and the copy it out again at the end.

Annoyances

  1. The Notification Centre is pretty obnoxious.
  2. Don't care for 'zsh'.  I installed home-brew and am running bash/ksh from there for all of my needs.  Change my login shell back to bash.
  3. Couldn't;t make a new directory called /app or /opt due to the Protection mechanism.  Had to edit /etc/synthetics.conf and add entries for the directories I wanted and then rebooted.  
  4. Auidiobook - See above.  Without iTunes there is no built in way to tag both MP4/M4A/M4B and MP3 files with the same program.

Benefits

  1. A clean install runs much better than my previous 6 year old base OS, which was upgraded and migrated each time.
  2. All of the old Mojave binaries that I did depend on are available in later versions. Homebrew/macports are your friends in this case.
  3. Installation of the printer drivers works perfectly well without any of that garbage bundles software that comes with printers.  My Brother Laser printer and Epson Scanner/Printer work perfectly well without having to install any of the vendor supplied software - and is probably more stable as well...



Wednesday 16 February 2022

Running a physical Mac Image under Vmware Fusion

A while(!) back, I wrote a document about copying a physical disk image into VMware Fusion.  This was for a older build of the various tools.  Recently I wanted to to the same thing but Using my old Mojave build as a VM and then re-installing from scratch a new version of Monterey and then slowly copying across the items I need and having the VM running Mojave as an emergency backup.

The process from the article Physical Yosemite Machine to VMware fusion Image is just the same, with the following changes.

Firstly, I'm using CCC version 5, which has changed from the version 4 documented on that page.

Changes to the steps on the above page for  Mojave install and CCC 5.0 are as follows:

  1. The disk image won't appear as 'not recognised by the computer', but will appear in Disk Utility.  Format the disk as specified, and keep the format as the same.

  1. - 22.  When selecting the remote machine you no longer need to copy a package across or change any settings. CCC 5 will check that it can copy files to the remote machine and will alert you as to any changes required.  It will ask for an account and to ensure remote login is turned on.
  1. Turn off the networking on the virtual machine setup before rebooting to stop any conflicts with naming etc.  e.g:
    • Disable Backblaze backup
    • Sign out of iCloud so that Photos, books, calendar, iTunes are not synced unneceesarily
    • Disable iTunes Match
    • Turn off LittleSnitch and other un-needed services.
    • Change the machine name
  • Reboot the VM without networking, perform the above and then turn the networking back on in the VMware settings before the 'final' reboot.

Thursday 18 November 2021

Homekit: Replacing the Ikea TRÅDFRI Gateway

For a couple of years I've been running a small automated homekit setup with the Ikea TRÅDFRI gateway.  It's been a bit of a rollercoaster ride, with everything working fine to start with, a few hiccups a bit later,  then getting better, and now just being almost unusable;  I can't connect to the gateway from the Ikea app, even though I'm only 10 feet away from the gateway, until I power cycle it, and then it only works for about a day before I need to do it all again.  Needless to say, at this point, none of the HomeKit automations work either.  An exercise in frustration.

A positive point of all the Ikea stuff I have bought (actually only bulbs and 2 power adapters) is that they are all Zigbee compliant, and so any Zigbee controller could be used.

One of my colleagues at work is a bit of a home automation expert, so I asked about his setup, and decided to bit the bullet and migrate to an all new setup using the Ikea devices.  It also means that I don't need to stick to Ikea in future in case I wanted to experiment with other vendors as well.  I had a spare Raspberry PI 3 lying around not doing much ( well, just running Pi-Hole for ad blocker and DHCP, but mostly idle) which I could use so decided to go this route.

I used the Zigbee2MQTT page as a starting point as to what adapter to get.  This was no mainly about what was available at the time, but I decided to go with the "Electrolama zig-a-zig-ah! (zzh!)" from the Zigbee2MQTT recommended devices, use a docker container on the Rasperry Pi to setup and run the Zigbee devices and use Home Assistant to act as a bridge between the devices and HomeKit on the Apple Ecosystem.

Zigbee Stick

The stick supplied doesn't come with any firmware on it at all, just code to flash the LEF when it is plugged in, so I needed first to flash the firmware.  There are instructions on the Electrolama web page for this for Windows and using a python program.  Also, when I ordered the stick, I didn't bother with the debug module.

Not having a windows machine around, I decided to go with the Python program which was remarkably easy on my Macbook Pro.  There are instructions on the page, specifically for Macintosh:


To run cc2538-bsl.py you need to install some extra python dependencies, python3 should already be shipped with macOS (Catalina onwards?). 

Download and extract cc2538-bsl: curl --output cc2538-bsl.zip https://codeload.github.com/JelmerT/cc2538-bsl/zip/master && unzip cc2538-bsl.zip 

Install required dependencies: $ /usr/bin/python3 -m pip install --user pyserial intelhex (As we cannot write to the system's location we need to install the dependencies with in the user location.)

After this, I put my stick in BSL mode by pressing the small button on the top and plugging into my USB port at the same time.  The device appeared as /dev/tty.usbserial*

I needed to download the firmware for co-ordinator usage as I was wanting to use this stand alone.  The link was on the main Zigbee2MQTT page, so I downloaded directly from there.  At this point, it was a simple case of running:

python3 cc2538-bsl.py -p /dev/tty.usbserial* -evw CC2652R_coordinator_20210708.hex

And the firmware.

Raspberry Pi

Docker.

As mentioned, earlier, I wanted to run the Automation in a container, so it would be way to start/stop and migrate to a new machine if necessary.

The Raspberry Pi, does not come with docker or docker-compose installed by default, but they are easy to install. https://dev.to/elalemanyo/how-to-install-docker-and-docker-compose-on-raspberry-pi-1mo gives a simple tutorial on installing docker and docker-compose.  It also has the benefit of restarting the containers, and hence the Homekit stuff if, for any reason, the Pi reboots.  I followed steps 1-6 and made sure everything was working successfully.

Next step was to create a directory for all of the zigbee/home automation code and config.

pi@bluepi:~ $ mkdir ~/zigbee

pi@bluepi:~ $ cd ~/zigbee

pi@bluepi:~/zigbee $ 

Hardware

The following are modifed from https://www.zigbee2mqtt.io/guide/getting-started/ with my local changes.


First, I plugged the stick into my Raspberry Pi. I then needed to determine the device that it was assigned to.


pi@bluepi:~/zigbee $ sudo dmesg

  [ deleted ]

usbcore: registered new interface driver ch341

usbserial: USB Serial support registered for ch341-uart

ch341 1-1.3:1.0: ch341-uart converter detected

usb 1-1.3: ch341-uart converter now attached to ttyUSB0

  [ deleted ]

pi@bluepi:~/zigbee $ ls -lL /dev/serial/by-id/

total 0

crw-rw---- 1 root dialout 188, 0 Nov 17 17:05 usb-1a86_USB_Serial-if00-port0

pi@bluepi:~/zigbee $ ls -l /dev/ttyUSB0

crw-rw---- 1 root dialout 188, 0 Nov 17 17:05 /dev/ttyUSB0

pi@bluepi:~/zigbee $ 




First I created a configuration file for docker-compose, using the device above.


pi@bluepi:~/zigbee $ cat docker-compose.yaml

version: '3.8'

services:

  mqtt:

    image: eclipse-mosquitto:2.0

    restart: unless-stopped

    volumes:

      - "./mosquitto-data:/mosquitto"

    ports:

      - "1883:1883"

      - "9001:9001"

    command: "mosquitto -c /mosquitto-no-auth.conf"


  zigbee2mqtt:

    container_name: zigbee2mqtt

    restart: unless-stopped

    image: koenkk/zigbee2mqtt

    volumes:

      - ./zigbee2mqtt-data:/app/data

      - /run/udev:/run/udev:ro

    ports:

      - 8080:8080

    environment:

      - TZ=Europe/London

    devices:

      - /dev/ttyUSB0:/dev/ttyUSB0

pi@bluepi:~/zigbee $ 

Since I'm running as the 'pi' users, and the permissions on the USB device above don't allow me to read/write to it, and I also had to run the usermod command to add 'pi' to the dialout group:

pi@bluepi:~/zigbee $ sudo usermod -aG dialout pi
pi@bluepi:~/zigbee $

Then a configuration file is needed. Yhis should be created under zigbee2mqtt-data/configuration.yaml after creating the zigbee2mqtt-data directory.  I added the homeassistant support in this file ready for later as well.

pi@bluepi:~/zigbee $ cat zigbee2mqtt-data/configuration.yaml

permit_join: true

mqtt:

  base_topic: zigbee2mqtt

  server: mqtt://mqtt

serial:

  port: /dev/ttyUSB0

frontend:

  port: 8080

homeassistant: true

advanced:

  network_key: GENERATE

pi@bluepi:~/zigbee $



Connect to the devices

Now it's time to start the docker images, and pair all the devices.  This is a simple case of running docker-compose.

The first time round, this may take some time as it will have to download images.  Subsequent calls to the command will be much faster.  The '-d' option to to disassociate from the terminal, so one they have started the shell prompt will appear.  After that we can run a 'tail -f' on the logs.  This can be interrupted with ^C at any time without stopping the docker image.

pi@bluepi:~/zigbee $ docker-compose up -d
    [ deleted ]
# Check the logs
pi@bluepi:~/zigbee $ docker-compose logs -f

Connect to the Zigbee web

The next stage was to connect to the Raspberry-Pi on port 8080 to ensure everything was working.  At this point it will be pretty much an empty page.

RePair all devices

At this point, I needed to re-pair all of the devices.  For the power adapters, it's a matter of pressing a small pin into the bottom for about 5 seconds until the LED flashes.  No problem.  The lights needed to be turned off and on 6 times at just the right amount of time between each on/off interval.  For some of the lights, this took about 5 or 6 attempts, but they all eventually went into pair mode and each appeared on the Zibgee web page.

As each was being paired, I changed the name of the device to something more reasonable.  At the end, the screen looked like this:


Set up Home Assistant and Homekit

I already added the homeassistant tag to the configuration file, above, so all this is needed is to add the Home Assitant information to the docker-compose file.  First, I created a directory "HA" in the zigbee directory for all of the Home Assistant configuration and storage.

Create a directory under zigbee and then add the following to the docker-compose file for HomeAssistant:

pi@bluepi:~/zigbee $ mkdir HA

pi@bluepi:~/zigbee $ cat docker-compose.yaml

  [deleted]

 homeassistant:

    container_name: homeassistant

    image: "ghcr.io/home-assistant/raspberrypi3-homeassistant:stable"

    volumes:

      - /home/pi/zigbee/HA:/config

      - /etc/localtime:/etc/localtime:ro

    restart: unless-stopped

# Example for HomeKit setup

    privileged: true

    network_mode: host

pi@bluepi:~/zigbee $


  1. Start up the docker images again and log on to Home assistant on the default port of 8123
  2. Set up account for the first time.

Now, back on the Raspberry-pi close all docker images down.

pi@bluepi:~/zigbee $ docker-compose down

Stopping homeassistant ... done

Stopping zigbee_mqtt_1 ... done

Stopping zigbee2mqtt   ... done

Removing homeassistant ... done

Removing zigbee_mqtt_1 ... done

Removing zigbee2mqtt   ... done

Removing network zigbee_default

pi@bluepi:~/zigbee $

Add 'homekit: and mqtt broker' information to homekit configuration.yaml file.

To edit this sudo will need to be used, as it will have been created by the root user.  i.e. sudo vi HA/configuration.yaml

pi@bluepi:~/zigbee $ cat HA/configuration.yaml 


# Configure a default setup of Home Assistant (frontend, api, etc)

default_config:


# Text to speech

tts:

  - platform: google_translate


group: !include groups.yaml

automation: !include automations.yaml

script: !include scripts.yaml

scene: !include scenes.yaml

homekit:

mqtt:

  broker: localhost

pi@bluepi:~/zigbee $ 

Start all the docker images again and then login to Home Assistant.

pi@bluepi:~/zigbee $ docker-compose up -d

Creating network "zigbee_default" with the default driver

Creating zigbee_mqtt_1 ... done

Creating homeassistant ... done

Creating zigbee2mqtt   ... done

pi@bluepi:~/zigbee $

Note: Notification about new devices with barcode will appear

Note: Scan in barcode on Phone Homekit app

Note: New devices will appear automatically.

Note: Move the devices to the correct rooms and carry on as normal, but just much faster now.

Conclusion

Take Aways:
  • Response from the new setup is much faster than with the original IKEA gateway.    Siri integration is more responsive as well.
  • A Raspberry PI 3 model B with 1G of memory is sufficient to run the Zigbee and Home Assistant software in a docker image, even with running Pi-Hole along side it.
  • It did take longer that it seems above; a few false starts trying to use the native Zigbee support in Home Assistant didn't work as expected, so moving to use MQTT messages was the better option.
  • Zigbee2MQTT has a net map feature that shows which devices are active and the string s of the signal between each one.
  • Home Assistant has a lot of scripting and reporting which can be used on the web page and there is an associated  iOS and Android app that can be used to control the devices as well as using HomeKit.

Summary for adding docker-compose.


In case the link forestalling docker-compose if offline, these are the steps.

Prerequisites

Raspberry Pi with a running Raspbian OS
  • SSH connection enabled


1. Update and Upgrade

First of all make sure that the system runs the latest version of the software.
Run the command:

sudo apt-get update && sudo apt-get upgrade

2. Install Docker

Now is time to install Docker! Fortunately, Docker provides a handy install script for that, just run:

curl -sSL https://get.docker.com | sh

3. Add a Non-Root User to the Docker Group

By default, only users who have administrative privileges (root users) can run containers. If you are not logged in as the root, one option is to use the sudo prefix.
However, you could also add your non-root user to the Docker group which will allow it to execute docker commands.

The syntax for adding users to the Docker group is:

sudo usermod -aG docker [user_name]

To add the permissions to the current user run:

sudo usermod -aG docker ${USER}

Check it running:

groups ${USER}

Reboot the Raspberry Pi to let the changes take effect.


4. Install Docker-Compose

Docker-Compose usually gets installed using pip3. For that, we need to have python3 and pip3 installed. If you don't have it installed, you can run the following commands:

sudo apt-get install libffi-dev libssl-dev
sudo apt install python3-dev
sudo apt-get install -y python3 python3-pip

Once python3 and pip3 are installed, we can install Docker-Compose using the following command:

sudo pip3 install docker-compose

5. Enable the Docker system service to start your containers on boot

This is a very nice and important addition. With the following command you can configure your Raspberry Pi to automatically run the Docker system service, whenever it boots up.

sudo systemctl enable docker

With this in place, containers with a restart policy set to always or unless-stopped will be re-started automatically after a reboot.